15.082J, 6.855J, and ESD.78J Sept 16, 2010 Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort.

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
15.082J & 6.855J & ESD.78J October 14, 2010 Maximum Flows 2.
15.082J & 6.855J & ESD.78J Shortest Paths 2: Bucket implementations of Dijkstra’s Algorithm R-Heaps.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 5 Shortest Paths: Label-Correcting Algorithms
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4.
Chapter 7 Maximum Flows: Polynomial Algorithms
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 421 Algorithms Richard Anderson Lecture 4. What does it mean for an algorithm to be efficient?
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
15.082J/6.855J/ESD.78J September 14, 2010 Data Structures.
15.082J and 6.855J and ESD.78J November 4, 2010 The Network Simplex Algorithm.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
& 6.855J & ESD.78J Algorithm visualizations Modified Label Correcting Algorithm.
15.082J, 6.855J, and ESD.78J September 21, 2010 Eulerian Walks Flow Decomposition and Transformations.
Chapter 4 Shortest Path Label-Setting Algorithms Introduction & Assumptions Applications Dijkstra’s Algorithm.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Spring 2015 Lecture 10: Elementary Graph Algorithms
15.082J and 6.855J and ESD.78J October 19, 2010 Max Flows 3 Preflow-Push Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
15.082J & 6.855J & ESD.78J September 23, 2010 Dijkstra’s Algorithm for the Shortest Path Problem.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
15.082J & 6.855J & ESD.78J October 7, 2010 Introduction to Maximum Flows.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
15.082J and 6.855J and ESD.78J The Successive Shortest Path Algorithm and the Capacity Scaling Algorithm for the Minimum Cost Flow Problem.
and 6.855J March 6, 2003 Maximum Flows 2. 2 Network Reliability u Communication Network u What is the maximum number of arc disjoint paths from.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
15.082J and 6.855J and ESD.78J Network Simplex Animations.
15.082J and 6.855J and ESD.78J October 21, 2010 Max Flows 4.
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.
CSC317 Graph algorithms Why bother?
EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008
Graph Algorithms Using Depth First Search
Elementary graph algorithms Chapter 22
Dijkstra’s Algorithm for the Shortest Path Problem
Dijkstra’s Algorithm for the Shortest Path Problem
Breadth first search animation
Lecture 4 Graph Search.
Lectures on Graph Algorithms: searching, testing and sorting
Basic Graph Algorithms
CS 583 Analysis of Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
EMIS 8374 Shortest Path Problems: Introduction Updated 9 February 2008
Shortest-Path Property 4.1
Richard Anderson Autumn 2016 Lecture 5
Lecture 13 Algorithm Analysis
Richard Anderson Winter 2009 Lecture 6
and 6.855J March 6, 2003 Maximum Flows 2
Network Optimization Depth First Search
Introduction to Algorithms
EMIS 8374 Search Algorithms Updated 9 February 2004
Network Optimization Topological Ordering
and 6.855J Topological Ordering
15.082J & 6.855J & ESD.78J Visualizations
Elementary graph algorithms Chapter 22
and 6.855J Depth First Search
Breadth first search animation
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Lecture 5 Graph Theory
Richard Anderson Winter 2019 Lecture 5
EMIS 8374 Search Algorithms Updated 12 February 2008
Max Flows 3 Preflow-Push Algorithms
Class 11 Max Flows Obtain a network, and use the same network to illustrate the shortest path problem for communication networks, the max flow.
Presentation transcript:

15.082J, 6.855J, and ESD.78J Sept 16, 2010 Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort

2 Overview Today: Different ways of searching a graph a generic approach breadth first search depth first search program verification data structures to support network search topological order u Fundamental for most algorithms considered in this subject

3 Searching a Directed Graph ALGORITHM SEARCH INPUT: A directed network G, and node s OUTPUT: The set S = {j : there is a directed path from s to j in G}. These are the nodes reachable from s. For each node j ∈ S\s, pred(j) is a node that precedes j on some path from s; e.g. (pred(2) = 1, pred(8) =

Marked nodes, admissible arcs A node is either marked or unmarked. Initially only node s is marked. If a node is marked, it is reachable from node s. An arc (i,j) ∈ A is admissible if node i is marked and j is not Marked nodes Admissible arcs

5 Scanning arcs CurrentArc(1) Scan through the arc list for the selected node, and keep track using current arc. Stop when an admissible arc is identified or when the arc list is fully scanned

6 Algorithm Search Initialize as follows: unmark all nodes in N; mark node s; pred(s) = 0; {that is, it has no predecessor} LIST = {s} while LIST ≠ ø do select a node i in LIST; if node i is incident to an admissible arc (i,j) then mark node j; pred(j) := i; add node j to the end of LIST; else delete node i from LIST The algorithm in the book also keeps track of the order in which nodes are marked.

Breadth first search 7 It is a breadth first search (bfs) if the selected node is the first node on LIST. Breadth First Search Animation

8 More on Breadth First Search Theorem. The breadth first search tree is the “shortest path tree”, that is, the path from s to j in the tree has the fewest possible number of arcs The numbers next to the nodes are the distances from node 1.

Depth first search 9 It is a depth first search (dfs) if the selected node is the last node on LIST. Depth First Search Animation

The depth first search tree Note that each induced subtree has consecutively labeled nodes. (The descendents are visited in order.)

Algorithm Analysis How does one prove that an algorithm is correct? How does one prove that it terminates in finite time? How does one obtain a tight upper bound on running time? 11

Some useful approaches Subdivide the algorithm into “chunks”. Invariants: properties that are true throughout the running of the algorithm Things that change: functions that increase monotonically every time the algorithm reenters the same loop. 12

13 Algorithm Search Initialize while LIST ≠ ø do select a node i in LIST; if node i is incident to an admissible arc (i,j) then mark node j; pred(j) := i; add node j to the end of LIST; else delete node i from LIST loop

14 Algorithm Invariants Select Node 2 LIST Invariants: whenever control of the program is at loop: 1.Any marked node is reachable from s. 2.All nodes on LIST are marked. 3.If a marked node j is not on LIST, then A(j) has been fully scanned.

Proving the correctness of the invariants An Important step in proving algorithm correctness: Prove that the algorithm invariants are true using induction. Prove that they are true after the initialization Assuming that they are true at the beginning of the k-th iteration of the while loop, prove that they are true at the beginning of the subsequent iteration of the while loop. 15

Things that change and proof of finiteness Things that change between successive times that the control of the program is at loop: 1. Either a new node is marked and added to LIST, or a new node is fully scanned and deleted from LIST. 16 Number of iterations of while loop is at most 2n. Therefore the algorithm terminates in O(n) calls of the “while loop.”

Proof of Correctness The algorithm terminates when LIST = ∅. 17 Let S = marked nodes. Let T = unmarked nodes t 6 s 3 s S T Then no arc (i,j) is directed from S to T. Otherwise, by Invariant 2, j would have been marked when (i, j) was scanned. By invariant 1, all nodes in S are reachable from s. By invariant 3, all arcs out of S have been scanned. Therefore, no node in T is reachable from s.

18 Cutset Theorem Corollary of algorithm’s correctness. There is no directed path from s to t if and only if the following is true: there is a partition of the node set N into subsets S and T = N - S such that there is no arc directed from a node in S to a node in T. S s T t

Running time analysis 19 Initialize. while LIST ≠ ø do select a node i in LIST; if node i is incident to an admissible arc (i,j) then mark node j; pred(j) := i; add node j to the end of LIST; else delete node i from LIST loop Total time spent in while loop (other than arc scans) O(1) time per loop < 2n iterations of the loop O(n) time in total Total time spent in scanning arcs O(1) time per arc scanned m arcs O(m) time in total. Running time: O(n + m)

20 Initialize begin unmark all nodes in N; mark node s; pred(s) = 0; {that is, it has no predecessor} LIST = {s} end Unmarking takes O(n) All else takes O(1)

21 Theorem. Algorithm search determines all nodes reachable from node s in O(n + m) time.

All U.S. Presidents have worn glasses True No President (before Obama) has been an only child True No President was a bachelor False. James Buchanan was a bachelor. George Washington grew marijuana on his Plantation. True Mental Break

George Washington’s false teeth were made of wood. False. They were made of whale bone. Three of the first 10 Presidents died on July 4. True. Thomas Jefferson, John Adams, James Monroe John Quincy Adams kept a pet alligator in the East Room of the White House True. Calvin Coolidge believed that the world was flat. False. But Andrew Jackson did. Mental Break

24 Finding all connected components in an undirected graph Breadth first search will find a connected component of an undirected graph in time proportional to the number of arcs in the component. (A component of an undirected graph is a maximally connected subgraph.) To find all components: Maintain a set U of unmarked nodes. Delete a node from U after it is marked. After each component is searched, select a node of U and begin a search. Running time O(1) per selection and deletion Comp 1 Comp 2 Comp 3

25 Proof of bfs Theorem Theorem. The breadth first search tree is the “shortest path tree”, that is, the path from s to j in the tree has the fewest possible number of arcs. Let d(j) be the fewest number of arcs on a path from s to j. Suffices to prove a 4 th invariant: 4. If d(i) < d(j), then i is marked before j. If the 4 th invariant is true, then nodes are marked in order of increasing distance from node s.

26 Select node 5 LIST If d(i) < d(j), then i is marked before j. (Note that no unmarked node has a distance that is less than d(4).)

27 Preliminary to Topological Sorting COROLLARY 2. If G has no directed cycle, then one can relabel the nodes so that for each arc (i,j), i < j. LEMMA. If each node has at least one arc going out, then the first inadmissible arc of a depth first search determines a directed cycle. COROLLARY 1. If G has no directed cycle, then there is a node in G with no arcs going out. Similarly, there is at least one node in G with no arcs coming in

28 Topological ordering INITIALIZE as follows: for all i ∈ N do indegree(i) := 0; for all (i,j) ∈ A do indegree(j) := indegree(j) + 1; LIST := ∅ ; next := 0; for all i ∈ N do if indegree(i) = 0, then LIST := LIST ∪ { i }; while LIST ≠ ∅ do select a node i from LIST and delete it from LIST; next := next + 1; order(i) := next; for all (i,j) ∈ A(i) do indegree(j) := indegree(j) – 1; if indegree(j) = 0 then LIST := LIST ∪ { j }; if next < n then the network contains a directed cycle else the network is acyclic and the order is topological Animation

29 Invariant For Topological Sorting A node is called marked when it receives an order. INVARIANT (at the beginning of the while loop) 1. indegree(i) is the number of arcs directed to i from nodes that are not marked. Thus if j is on LIST, then there are no arcs into node j from unmarked nodes. If the algorithm ends before labeling all nodes, then there is a directed cycle in the unmarked nodes. Every unmarked node has at least one incoming arc, and so there is a directed cycle.

30 More on Topological Sorting Runs in O(n+m) time. Useful starting point for many algorithms that involve acyclic graphs.

31 Summary on Graph Search u Graph Search u Finds all nodes reachable from s in O(m) time. u Determine the connected components of an undirected graph. u Breadth first search u Depth first search Algorithm Validation (proofs of correctness). Prove invariants using induction Establish things that change

32 Summary on Graph Search u Topological sort (or order); Running time is O(n+m) using simple data structures and algorithms. Very important for preprocessing.

MITOpenCourseWare J / 6.855J / ESD.78J Network Optimization Fall 2010 For information about citing these materials or our Terms of Use, visit: