Lecture 11 Graph Algorithms

Slides:



Advertisements
Similar presentations
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Advertisements

Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Algorithms and Data Structures
Breadth-First and Depth-First Search
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
CSC 331: Algorithm Analysis Decompositions of Graphs.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Graph Search Applications, Minimum Spanning Tree
CSC 172 DATA STRUCTURES.
Breadth-First Search (BFS)
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Graphs Chapter 20.
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Lecture 11 Graph Algorithms
Graph Algorithms BFS, DFS, Dijkstra’s.
CSC317 Graph algorithms Why bother?
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Depth-First Search.
CS120 Graphs.
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Spanning Trees Longin Jan Latecki Temple University based on slides by
CSC 172 DATA STRUCTURES.
Graph Search Applications
Lecture 15 CSE 331 Sep 29, 2014.
CSE 421: Introduction to Algorithms
CS 3343: Analysis of Algorithms
Lecture 14 CSE 331 Sep 30, 2016.
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Chapter 11 Graphs.
Yan Shi CS/SE 2630 Lecture Notes
Lecture 13 CSE 331 Sep 27, 2017.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
Lecture 14 CSE 331 Sep 29, 2017.
COMP171 Depth-First Search.
Richard Anderson Winter 2009 Lecture 6
Depth-First Search CSE 2011 Winter April 2019.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Lecture 6 Graph Traversal
Lecture 12 CSE 331 Sep 22, 2014.
Depth-First Search CSE 2011 Winter April 2019.
Chapter 16 1 – Graphs Graph Categories Strong Components
Spanning Trees Longin Jan Latecki Temple University based on slides by
CSE 417: Algorithms and Computational Complexity
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Lecture 10 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Richard Anderson Autumn 2015 Lecture 6
Presentation transcript:

Lecture 11 Graph Algorithms

Type of Edges Tree/Forward: pre(u) < pre(v) < post(v) < post(u) Back: pre(v) < pre(u) < post(u) < post(v) Cross: pre(v) < post(v) < pre(u) < post(u)

Application 1 – Cycle Finding Given a directed graph G, find if there is a cycle in the graph. What edge type causes a cycle?

Algorithm DFS_cycle(u) Mark u as visited Mark u as in stack FOR each edge (u, v) IF v is in stack (u,v) is a back edge, found a cycle IF v is not visited DFS_visit(v) Mark u as not in stack. DFS FOR u = 1 to n DFS_visit(u)

Application 2 – Topological Sort Given a directed acyclic graph, want to output an ordering of vertices such that all edges are from an earlier vertex to a later vertex. Idea: In a DFS, all the vertices that can be reached from u will be reached. Examine pre-order and post-order Pre: a c e h d b f g Post: h e d c a b g f Output the inverse of post-order!

Breadth First Search Visit neighbor first (before neighbor’s neighbor). BFS_visit(u) Mark u as visited Put u into a queue WHILE queue is not empty Let x be the head of the queue FOR all edges (x, y) IF y has not been visited THEN add y to the queue Mark y as visited Remove x from the queue BFS FOR u = 1 to n

Breadth First Search Tree IF y is added to the queue while examining x, then (x, y) is an edge in the BFS tree 1 1 2 3 2 3 4 4 5 5

BFS and Queue BFS Order: The order that vertices enter (and exit) the queue 1 2 3 4 5 1 2 3 4 5

Application – Shortest Path Given a graph, vertices (u, v), find the path between (u, v) that minimizes the number of edges. Claim: The BFS tree rooted at u contains shortest paths to all vertices reachable from u.

Running Time of Graph Traversal Algorithms For both BFS and DFS Enumerating all starting point takes O(n) time. In the recursive call/BFS_vsit, each edge is processed at most twice. This is O(m) time. Total running time = O(n+m) For connected graphs: m > n - 2, so total running time is O(m). If we just want to start at a fixed location, then the running time is also O(m).