61B part 5..4 Graph
Trees
- A set of connected nodes (or vertices). We use both terms interchangeably.
- A set of edges that connect those nodes. No edges can form a cycle.
- Constraint: There is exactly one path between any two nodes.
function: speeding up searching for items
- Constraint: There is exactly one path between any two nodes.
Tree Traversal
what are some natural ways to ‘traverse’ (iterate) through a tree?
- Level order traversal. Breadth First Search (BFS)
- Depth-First traversals –– of which there are three: pre-order, in-order and post-order. (DFS)
pre-order
1 | preOrder(BSTNode x) { |
in-order
instead of visiting (aka printing) first, we’ll first visit the left branch. Then we’ll print. Then we’ll visit the right branch.
1 | inOrder(BSTNode x) { |
post-order
1 | postOrder(BSTNode x) { |
Graphs
definition
A graph consists of:
- A set of nodes (or vertices)
- A set of zero of more edges, each of which connects two nodes.

note that all trees are also graphs, but not all graphs are trees.
simple graphs & multigraphs
Simple graphs: do not have loops & multiple ways from one to another
more categories

problems
- s-t Path: Is there a path between vertices s and t?
- Connectivity: Is the graph connected, i.e. is there a path between all vertices?
- Biconnectivity: Is there a vertex whose removal disconnects the graph?
- Shortest s-t Path: What is the shortest path between vertices s and t?
- Cycle Detection: Does the graph contain any cycles?
- Euler Tour: Is there a cycle that uses every edge exactly once?
- Hamilton Tour: Is there a cycle that uses every vertex exactly once?
- Planarity: Can you draw the graph on paper with no crossing edges?
- Isomorphism: Are two graphs isomorphic (the same graph in disguise)? // a million-dollar problem!
…
let’s solve the first problem: if s and t connected?
triversal
BFS
1 | Initialize the fringe, an empty queue |
fringe: the data structure we are using to store the nodes on the frontier of our traversal’s discovery processedgeTo[...]: a map that helps us track how we got to node n; we got to it by following the edge from v to to n.distTo[...]: helps us track how far n is from the starting vertex
DFS
1 | mark s // i.e., remember that you visited s already |
1 | Initialize the fringe, an empty stack |
graphs API
1 | public class Graph { |
we want to get quick answers to these questions:
- Are given vertices
uandvadjacent? - Is vertex
vincident to a particular edgee? - What vertices are adjacent to
v? - What edges are incident to
v?
so we use adjacency list for the data structure.
Adjacency List
![[Pasted image 20240514103306.png]]
An array is created with each vertice in graph, each of them points to a list that including all of the edges of the vertice
Shortest path
BFS or DFS will never have the ability to get the shortest path in the least time always, so some algs are developed.
Dijkstra’s Algorithm
You are on campus and playing a game with your friends around Hearst Memorial Mining Building. You start at the location s and you want to go to the location t.
![[Pasted image 20240514110735.png]]
BFS will yield a route of length 330m.
![[Pasted image 20240514110800.png]]
Goal: minimize the sum of the weights of the edges on the selected path.
the shortest paths tree from a source s can be created in the following way:
- For every vertex
v(which is nots) in the graph, find the shortest path fromstov. - “Combine”/“Union” all the edges that you found above.
the “Shortest Path Tree” will always be a tree. Why? For every node, there was exactly one “parent” in the edgeToedgeTo array that we have maintained in our original solution.
working
How does it work?
- Create a priority queue.
- Add
sto the priority queue with priority 00. Add all other vertices to the priority queue with priority ∞∞. - While the priority queue is not empty: pop a vertex out of the priority queue, and relax all of the edges going out from the vertex.
relax
vertex: v
Look at your current best distance to w from the source, call it curBestDistToW. Now, look at your curBestDistToV+weight(v,w) (let’s call it potentialDistToWUsing).
Is potentialDistToWUsing better, i.e., smaller than curBestDistToW? In that case, set curBestDistToW=potentialDistToWUsingV, and update the edgeToW to be v
implementation
1 | public class Dijkstra { |
disadvantages
![[Pasted image 20240514113547.png]]
Suppose you’re at that vertex labeled 34. Now you’re going to try to relax all your edges. You have only one outgoing edge from yourself to 33 with weight −67. Ah, but note: vertex 33 is already visited (it’s marked with white.) So… we don’t relax it. (Recall the pseudocode for the relax method.)
Now we go home thinking that the shortest distance to 33 is 82 (marked in pink.) But really, we should have taken the path through 34 because that would have given us a distance of 101−67=34.
Dijkstra’s algorithm is not guaranteed to be correct for negative edges. It might work… but it isn’t guaranteed to work.
short-circuit
if a vertice is popped out from the queue, the algs is stopped (short-circuited).
A* algs
let’s consider the drawbacks of Dijkstra’s algorithm: if you want to seek a road from the middle of the US to the east, you will must have to make a circle first, lowering the effeciency. How can we let computer know that it is better to move towards east then west?
The A-star
![[Pasted image 20240514221630.png]]
f(n)=g(n)+h(n)f(n): the priority of node ng(n): the cost of node n from the starth(n): the estimated cost of n to final ones
implementation
1 | * 初始化open_set和close_set; |
The priority of nodes are usually computed by the specific algs for specific cases.
minimum spanning trees
how to find a circle in a graph?
spanning trees
an undirected graph, a spanning tree T is a subgraph of G, where T :
- is connected
- is acyclic (not circled)
- includes all of the vertices
MST
the spanning trees that its sum of priority is the least ones.
cut property
cut: an assignment of a graph’s node to 2 setscorssing edge: an edge which connects the 2 node from 2 different sets
![[Pasted image 20240515103058.png]]
given any cut, minimum weight crossing edge is in the MST. (can be easily proved.)
Prim’s algs
- Start from one node
- add shortest edge that has one node inside the MST under construction and repeat the process
implementation
using priority heap queues, storing vertices in order of distance from tree
repeat(remove closest vertex v from PQ, and relax all edges pointing from v)
1 | public class PrimMST { |
VS Dijkstra’s algs
the same except:
- visiting order: Prim’s in order of distance from the MST, while D’s in order of distance from source
relaxation: - Dijkstra’s considers an edge better based on distance to source
- Prim’s a dege better based on distance to tree
complexity
O(ElogV)
Kruskal’s algs
ordering the edges by their priorities, and add the edge from smallest to biggest without circle (using union-find datastructure).
implementation
1 | public class KruskalMST { |
