Chapter 8: Graphs (part 2)
Objectives
Looking ahead – in this chapter, we’ll consider
Data Structures and Algorithms in C++, Fourth Edition
Graph Representation
Data Structures and Algorithms in C++, Fourth Edition
Graph Representation -- Pros & Cons?
Data Structures and Algorithms in C++, Fourth Edition
Graph Representation -- Pros and Cons?
Data Structures and Algorithms in C++, Fourth Edition
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
void Tree::preorder_search(Node & N) { N.visited = true; for (each Node C that is a child of N) preorder_search(C); } |
void Graph::DFS(Vertex & V) { V.visited = true; for (each Vertex W that is a neighbor of V) if (!W.visited) DFS(W); } |
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
void Tree::preorder_search(Node & N) { N.visited = true; for (each Node C that is a child of N) preorder_search(C); } |
|
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
void Tree::preorder_search(Node & N) { N.visited = true; for (each Node C that is a child of N) preorder_search(C); } |
void Graph::DFS(Vertex & V) { V.visited = true; for (each Vertex W neighboring V) if (!W.visited) DFS(W); } |
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
void Tree::preorder_search(Node & N) { N.visited = true; for (each Node C that is a child of N) preorder_search(C); } |
void Graph::DFS(Vertex & V) { V.visited = true; for (each Vertex W neighboring V) if (!W.visited) DFS(W); } |
Assuming neighbors are visited in alphabetical order, what is the overall order of visitation of DFS(a)? DFS(g)?
Graph Traversals without Recursion
Data Structures and Algorithms in C++, Fourth Edition
|
// all vertices are initially unvisited void Graph::DFS(Vertex & start) { Stack<Vertex> S; S.push(start); while (!S.empty()) { Vertex V = S.top(); S.pop(); if (!V.visited) { V.visited = true; for (each Vertex W neighboring V) if (!W.visited) S.push(W); } } } |
Assuming neighbors are visited in alphabetical order, what is the overall order of visitation of DFS(a)? DFS(g)?
Graph Traversals
Data Structures and Algorithms in C++, Fourth Edition
How to modify DFS() to make it path search between two
specific vertices? I.e., how to solve a maze?
Graph Traversals (continued)
Data Structures and Algorithms in C++, Fourth Edition
Eulerian and Hamiltonian Graphs
Data Structures and Algorithms in C++, Fourth Edition
Eulerian and Hamiltonian Graphs�(continued)
Fig. 8.34 Finding an Eulerian cycle
Data Structures and Algorithms in C++, Fourth Edition
Eulerian and Hamiltonian Graphs�(continued)
Data Structures and Algorithms in C++, Fourth Edition
Graph Traversals: Breadth-First Search
Data Structures and Algorithms in C++, Fourth Edition
Breadth-First Search without Recursion
Data Structures and Algorithms in C++, Fourth Edition
|
// all vertices are initially unvisited void Graph::BFS(Vertex & start) { Queue<Vertex> Q; Q.push(start); while (!Q.empty()) { Vertex V = Q.top(); Q.pop(); if (!V.visited) { V.visited = true; for (each Vertex W neighboring V) if (!W.visited) Q.push(W); } } } |
Assuming neighbors are visited in alphabetical order, what is the overall order of visitation of BFS(a)? BFS(g)?
Breadth-First Search: Shortest Path
Data Structures and Algorithms in C++, Fourth Edition
void Graph::BFS_ShortestPath(Vertex & start) { Queue<Vertex> Q; for (each Vertex V in the graph) V.dist = INFINITY; start.dist = 0; start.path = NULL; // build finish to start SLL
Q.enqueue(start); while (!Q.empty()) { Vertex V = Q.dequeue(); for (each Vertex W neighboring V) if (W.dist == INFINITY) { // aka not visited W.dist = V.dist + 1; W.path = V; // record predecessor Q.enqueue(W); } } } |
Shortest Paths
Data Structures and Algorithms in C++, Fourth Edition
Shortest Paths (continued)
Data Structures and Algorithms in C++, Fourth Edition
Dijkstra's Shortest Path Algorithm
Data Structures and Algorithms in C++, Fourth Edition
void Graph::Dijkstra(Vertex & start) { PriorityQueue<Vertex> PQ; // min PQ on vertex dist for (each Vertex V in the graph) { if (V == start) V.dist = 0; // so start will be chosen first from PQ else V.dist = INFINITY; V.checked = false; V.path = NULL; PQ.insert(V); }
while (true) { Vertex V = PQ.removeMin(); if (!V) return; // PQ empty, we are done V.checked = true; for (each Vertex W neighboring V) if (!W.checked && W.dist > V.dist + weight(V, W)) { W.dist = V.dist + weight(V, W); // decrease W.dist, which W.path = V; // changes PQ placement } } } |
Example: Dijkstra's Shortest Path Algorithm
Data Structures and Algorithms in C++, Fourth Edition
Data Structures and Algorithms in C++, Fourth Edition
Execution of Dijkstra(d)
void Graph::Dijkstra(Vertex & start) { PriorityQueue<Vertex> PQ; // min PQ on vertex dist for (each Vertex V in the graph) { if (V == start) V.dist = 0; // so start will be chosen first from PQ else V.dist = INFINITY; V.checked = false; V.path = NULL; PQ.insert(V); }
while (true) { Vertex V = PQ.removeMin(); if (!V) return; // PQ empty, we are done V.checked = true; for (each Vertex W neighboring V) if (!W.checked && W.dist > V.dist + weight(V, W)) { W.dist = V.dist + weight(V, W); // decrease W.dist, which W.path = V; // changes PQ placement } } } |
Example: Dijkstra's Shortest Path Algorithm
Data Structures and Algorithms in C++, Fourth Edition
Data Structures and Algorithms in C++, Fourth Edition
void Graph::Dijkstra(Vertex & start) { PriorityQueue<Vertex> PQ; // min PQ on vertex dist for (each Vertex V in the graph) { if (V == start) V.dist = 0; // so start will be chosen first from PQ else V.dist = INFINITY; V.checked = false; V.path = NULL; PQ.insert(V); }
while (true) { Vertex V = PQ.removeMin(); if (!V) return; // PQ empty, we are done V.checked = true; for (each Vertex W neighboring V) if (!W.checked && W.dist > V.dist + weight(V, W)) { W.dist = V.dist + weight(V, W); // decrease W.dist, which W.path = V; // changes PQ placement } } } |
Spanning Trees
Fig. 8.14 A graph representing (a) the airline connections between
seven cities and (b–d) three possible sets of connections
Data Structures and Algorithms in C++, Fourth Edition
Spanning Trees (continued)
Data Structures and Algorithms in C++, Fourth Edition
Spanning Trees (continued)
Data Structures and Algorithms in C++, Fourth Edition
Spanning Trees (continued)
At the termination of the algorithm, the forest forms a minimum spanning forest of the graph. If the graph is connected, the forest has a single component and forms a minimum spanning tree.
Data Structures and Algorithms in C++, Fourth Edition
Spanning Trees (continued)
algorithm Kruskal(G) is
F:= ∅
for each v ∈ G.V do
MAKE-SET(v)
for each (u, v) in G.E ordered by weight(u, v), increasing do
if FIND(u) ≠ FIND(v) then
F:= F ∪ {(u, v)} ∪ {(v, u)}
UNION(FIND-SET(u), FIND-SET(v))
return F
Data Structures and Algorithms in C++, Fourth Edition
Spanning Trees (continued)
algorithm Kruskal(G) is
F:= ∅
for each v ∈ G.V do
MAKE-SET(v)
for each (u, v) in G.E ordered by weight(u, v), increasing do
if FIND(u) ≠ FIND(v) then
F:= F ∪ {(u, v)} ∪ {(v, u)}
UNION(FIND-SET(u), FIND-SET(v))
return F
Data Structures and Algorithms in C++, Fourth Edition