Lecture 19: Applications of DFS
Total Page:16
File Type:pdf, Size:1020Kb
Data Structures in Java Lecture 19: Applications of DFS 11/30/2015 Daniel Bauer 1 Contents • Applications of DFS • Euler Circuits • Biconnectivity in Undirected Graphs. • Finding Strongly Connected Components for Directed Graphs. 2 Contents • Applications of DFS • Euler Circuits • Biconnectivity in Undirected Graphs. • Finding Strongly Connected Components for Directed Graphs. 3 Draw this Figure Without lifting your pen off the paper. 4 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 5 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 6 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 7 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 8 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 9 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 10 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 11 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 12 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. 13 Euler Paths • An Euler Path is a path through an undirected graph that visits every edge exactly once. This graph does not have an Euler Path. 14 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. This graph does NOT have an Euler Circuit 15 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 16 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 17 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 18 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 19 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 20 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 21 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 22 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 23 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 24 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 25 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. 26 Euler Circuit • An Euler Circuit is an Euler path that begins and ends at the same node. This graph does not have an Euler Path. 27 Necessary Condition for Euler Circuits • Observation: • Once we enter v, we need another edge to leave it. • If v has an odd number of edges, the last time we enter v, we will be stuck! 3 28 Necessary Condition for Euler Circuits • Observation: • Once we enter v, we need another edge to leave it. • If v has an odd number of edges, the last time we enter v, we will be stuck! 2 29 Necessary Condition for Euler Circuits • Observation: • Once we enter v, we need another edge to leave it. • If v has an odd number of edges, the last time we enter v, we will be stuck! 1 30 Necessary Condition for Euler Circuits • Observation: • Once we enter v, we need another edge to leave it. • If v has an odd number of edges, the last time we enter v, we will be stuck! • For an Euler Circuit to exist in a graph, all vertices need to have even degree (even number of edges). 0 31 Necessary Condition for Euler Paths • For an Euler Path to exist in a graph, exactly 0 or 2 vertices must have odd degree. • Start with one of the odd vertices. • End in the other one. 3 3 32 Necessary Condition for Euler Paths • For an Euler Path to exist in a graph, exactly 0 or 2 vertices must have odd degree. • Start with one of the odd vertices. • End in the other one. 3 3 33 Conditions for Euler Paths and Circuits • For an Euler Circuit to exist in a graph, all vertices need to have even degree (even number of edges). • For an Euler Path to exist in a graph, exactly 0 or 2 vertices need to have odd degree. • These conditions are also sufficient! (i.e. every graph that contains only vertices of even degree has an Euler circuit). (Hierholzer, 1873 ) 34 Seven Bridges of Königsberg Leonhard Euler, 1735 The city of Königsberg in Prussia (now Kaliningrad, Russia) was set on both sides of the Pregel River, and included two large islands which were connected to each other and the mainland by seven bridges. Euler’s Problem: Find a walk through the city that crosses every bridge exactly once! Source:35 Wikipedia Seven Bridges of Königsberg Leonhard Euler, 1735 36 Seven Bridges of Königsberg Leonhard Euler, 1735 3 5 3 3 There is no Euler Path in this graph. 37 Finding Euler Circuits • Start with any vertex s. First, using DFS find any circuit starting and ending in s. Mark all edges on the circuit as visited. • Because all vertices have even degree, we are guaranteed to not get stuck before we arrive at s again. • While there are still edges in the graph that are not market visited: • Find the first vertex v on the circuit that has unvisited edges. • Find a circuit starting in v and splice this path into the first circuit 38 Finding Euler Circuits 1 2 3 2 4 2 4 4 7 5 4 4 6 4 8 9 10 2 2 4 39 Finding Euler Circuits 1 2 3 1 3 2 4 4 7 5 4 4 6 4 8 9 10 2 2 4 40 Finding Euler Circuits 1 2 3 1 2 2 4 3 7 5 4 4 6 4 8 9 10 2 2 4 41 Finding Euler Circuits 1 2 3 1 2 2 4 2 7 5 4 4 6 3 8 9 10 2 2 4 42 Finding Euler Circuits 1 2 3 1 2 2 4 2 7 5 4 3 6 2 8 9 10 2 2 4 43 Finding Euler Circuits 1 2 3 0 2 2 4 2 7 5 4 2 6 2 8 9 10 2 2 4 1 2 4 6 5 1 44 Finding Euler Circuits 1 2 3 0 2 2 4 2 7 5 4 2 6 2 8 9 10 2 2 4 1 2 4 6 5 1 45 Finding Euler Circuits 1 2 3 0 0 0 4 2 7 5 2 2 7 6 10 3 2 2 6 0 8 9 10 2 2 2 1 2 4 6 5 1 46 Finding Euler Circuits 1 2 3 0 0 0 4 2 7 5 2 2 7 6 10 3 2 2 6 0 8 9 10 2 2 2 1 4 6 5 1 47 Finding Euler Circuits 1 2 3 0 0 0 4 0 7 5 0 7 4 5 8 9 10 7 0 6 0 8 9 10 0 0 0 1 2 7 6 10 3 2 4 6 5 1 48 Finding Euler Circuits 1 2 3 0 0 0 4 0 7 5 0 7 4 5 8 9 10 7 0 6 0 8 9 10 0 0 0 1 2 6 10 3 2 4 6 5 1 49 Hamiltonian Cycle • A Hamiltonian Path is a path through an undirected graph that visits every vertex exactly once (except that the first and last vertex may be the same). • A Hamiltonian Cycle is a Hamiltonian Path that starts and ends in the same node. 50 No hamiltonian path. Hamiltonian Cycle • We can check if a graph contains an Euler Cycle in linear time. • Surprisingly, checking if a graph contains a Hamiltonian Path/Cycle is much harder! • No polynomial time solution (i.e. O(Nk) ) is known. 51 No hamiltonian path. Contents • Applications of DFS • Euler Circuits • Biconnectivity in Undirected Graphs. • Finding Strongly Connected Components for Directed Graphs. 52 Connectivity • An undirected graph is connected if there is a path from every vertex to every other vertex. • Test for connectivity: See if DFS can reach all vertices. unconnected53 graph Biconnectivity • A graph is biconnected if there is no single vertex v, such that removing v will disconnect the remaining graph. A B A B D E C D F C biconnected G E 54 not biconnected Biconnectivity • A graph is biconnected if there is no single vertex v, such that removing v will disconnect the remaining graph. A B A B D E D F C biconnected G E 55 not biconnected Biconnectivity • A graph is biconnected if there is no single vertex v, such that removing v will disconnect the remaining graph. A B A B D E C D F C biconnected G E 56 not biconnected Biconnectivity • A graph is biconnected if there is no single vertex v, such that removing v will disconnect the remaining graph. A B A B D E C F C biconnected G E 57 not biconnected Biconnectivity • A graph is biconnected if there is no single vertex v, such that removing v will disconnect the remaining graph.