Graph Apps
Graph Apps
CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University
Applications
Depth-first search Biconnectivity Euler circuits Strongly-connected components
Depth-First Search
Recursively visit every vertex in the graph Considers every edge in the graph
Assumes undirected edge (u,v) is in us and vs adjacency list
DFS () ;; graph G=(V,E) foreach v in V if (! v.visited) then Visit (v) Visit (vertex v) v.visited = true foreach w adjacent to v if (! w.visited) then Visit (w) A B D C E
DFS Applications
Undirected graph
Test if graph is connected
Run DFS from any vertex and then check if any vertices not visited
DFS Applications
Remembering the DFS traversal order is important for many applications Let the edges (v,w) added to the DF spanning tree be directed Add a directed back edge (dashed) if
w is already visited when considering edge (v,w), and v is already visited when considering reverse edge (w,v)
A B D C E B A D C
5
Biconnectivity
A connected, undirected graph is biconnected if the graph is still connected after removing any one vertex
I.e., when a node fails, there is always an alternative route
If a graph is not biconnected, the disconnecting vertices are called articulation points
Critical points of interest in many applications
Biconnected? Articulation points?
6
Let Low(v) = lowest-numbered vertex reachable from v using 0 or more spanning tree edges and then at most one back edge
Low(v) = minimum of
Num(v) Lowest Num(w) among all back edges (v,w) Lowest Low(w) among all tree edges (v,w)
Original Graph
Original Graph
10
Last two post-order traversals can be combined In fact, all three traversals can be combined in one recursive algorithm
11
Implementation
12
13
14
Euler Circuits
Puzzle challenge
Can you draw a figure using a pen, drawing each line exactly once, without lifting the pen from the paper? And, can you finish where you started?
15
Euler Circuits
Solved by Leonhard Euler in 1736 using a graph approach (DFS)
Also called an Euler path or Euler tour Marked the beginning of graph theory
16
17
Graph with two odd-degree vertices can have an Euler tour (not circuit) Any other graph has no Euler tour or circuit
18
20
Start at vertex 4. Suppose DFS visits 4, 1, 3, 7, 4, 11, 10, 7, 9, 3, 4. Splicing into previous path: 5, 4, 1, 3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5.
21
Start at vertex 3. Suppose DFS visits 3, 2, 8, 9, 6, 3. Splicing into previous path: 5, 4, 1, 3, 2, 8, 9, 6, 3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5.
22
Start at vertex 9. Suppose DFS visits 9, 12, 10, 9. Splicing into previous path: 5, 4, 1, 3, 2, 8, 9, 12, 10, 9, 6, 3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5. No more un-traversed edges, so above path is an Euler circuit.
23
Analysis
Each edge considered only once Running time is O(|E|+|V|)
24
25
DF Spanning Forest
Three types of edges in DF spanning forest
Back edges linking a vertex to an ancestor Forward edges linking a vertex to a descendant Cross edges linking two unrelated vertices
Graph: A B D C E DF Spanning Forest: back B cross A D C
26
DF Spanning Forest
forward
Topological sort
Reverse post-order traversal of DF spanning forest
28
Strongly-Connected Components
A graph is strongly connected if every vertex can be reached from every other vertex A strongly-connected component of a graph is a subgraph that is strongly connected Would like to detect if a graph is strongly connected Would like to identify strongly-connected components of a graph Can be used to identify weaknesses in a network General approach: Perform two DFSs
29
Strongly-Connected Components
Algorithm
Perform DFS on graph G
Number vertices according to a post-order traversal of the DF spanning forest
Strongly-Connected Components
Graph G Graph Gr
31
Running time
Two executions of DFS O(|E|+|V|)
32
Summary
Graphs one of the most important data structures Studied for centuries Numerous applications Some of the hardest problems to solve are graph problems
E.g., Hamiltonian (simple) cycle, Clique
33