Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 (parent) of v. A vertex is discovered at most once. Run time: n = number of vertices m = number of edges Each vertex is enqueued and dequeued at most once (why? They are never white again). Costs? Per vertex 1, n times O(n). Scans adjacency list only when dequeued. Therefore, each adjacency list only at most once. Sum of all adjacency lists depends on number of edges O(m). Total O(n + m) (i.e. linear in number of vertices and edges)

2 CSC317 2 Why are we doing this? BFS allows us to find shortest paths! Shortest path distance from s to v :  (s,v) … minimum number of edges from any path from vertex s to v ( ∞ if no paths exist) Theorem: Let G = (V,E) be a (un)directed graph. Let be an abitrary vertex. Then, for any edge. How are we going to proof this? If u is reachable from s, then so is v. In this case the shortest path from s to v can not be longer than the shortest path from s to u followed by the edge (u,v). So, the inequality holds. (Forgot something?) If u is not reachable from s then  (s,u)=∞ and the inequality holds.

3 CSC317 3 Main Theorem: Let G = (V,E) be a (un)directed graph and suppose that BFS is run on G from a given source vertex. Upon termination, for each vertex the value v.d computed by BFS satisfies v.d... Is the shortest path, shortes number of edges from source s to v. Proof: We use induction on the number of ENQUEUE operations. Our inductive hypothesis is for all. The basis of the induction is the situation immediately after enqueuing s in line 9 of BFS. Does the inductive hypothesis in the beginning hold? and for all. For the inductive step, consider a white vertex v that is discovered during the search for vertex u, implying that. From the above theorem (and assignment in line 15) we get

4 CSC317 4 What does that mean? Vertex v is then enqueued, and it is never enqueued again. I.e. clause of lines 14–17 is executed only for white vertices. Thus, the value of v.d never changes again!

5 CSC317 5 Graph algorithms part 2 Depth First Search (DFS) Search deeply first... (What’s the difference to BFS?) Explores edges out of most recently discovered vertex so long as there are still unexplored edges leaving it... and then backtracks (like searching in a maze). Run time as in BFS will be linear in number of vertices and edges O(n+m) Applications: -topological sort in directed acyclic graphs (sequences of events where one comes before another; later); -strongly connected components (later). https://www.cs.usfca.edu/~galles/visualization/DFS.html

6 CSC317 6 Simple example of DFS ordering showing main idea and distinguishing from BFS: s a b c d e f 1 23 5 4 7 6 The predecessor subgraph formed by DFS is composed of several trees because the search may repeat from multiple sources.

7 CSC317 7 - One way to implement: Stack instead of queue. Last in first out (LIFO). But often implemented recursively.

8 CSC317 8 Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first search. // start time // we discovered u // finish time we are finished going through all of u’s neighbors

9 CSC317 9

10 10 Run time DFS: O(n + m) with n number vertices and m number edges. Procedure DFS-Visit is called exactly once for each vertex (only if white and then changed). During the DFS-Visit all adjacent nodes of each vertex are used, so overall includes all edges. Some properties of DFS: 1.) Every vertex v that gets discovered between start of u and end of u, is a child of u in the DFS tree. The v will also finish before u does (see path from u to v to y to x) This can be seen in recursion: DFS-Visit( u ) called first; and then calls DFSvisit( v ), which finishes first (last in first out, as in a stack) 2.) There is never an edge from a black to a white node (because if a node is black, we already visited all its neighbors). 3.) White path theorem: vertex v is a descendant of vertex u if and only if when the search discovers u, there is a path from u to v consisting entirely of white vertices.

11 CSC317 11 4.) Disjoint trees, contained trees, and parenthesis theorem: Consider vertices u and v in a graph G(V,E). Then one of the following conditions hold: intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, interval [u.d, u.f] is contained entirely within [v.d, v.f], and u is a descendant of v (which means u was discovered after v ), or interval [v.d, v.f] is contained entirely within [u.d, u.f], and v is a descendant of u (which means v was discovered after u ) (vice versa)

12 CSC317 12 Application DFS: topological sort: A directed edge from u to v, means that v happens after u. Topological order: All directed edges only go forward (needs to be acyclic graph DAG). Useful for sorting problems with tasks where one needs to be done before another. Again linear time in vertices and edges O(n+m), since doing DFS. Definitions: A topological sort of a dag G = (V,E) is a linear ordering of all its vertices such that if G contains an edge (u,v) then u appears before v in the ordering. (If the graph contains a cycle, then no linear ordering is possible.)

13 CSC317 13

14 CSC317 14 How can we do that? We can perform topological sort in time Θ (V+E) since depth-first search takes Θ (V+E) time and it takes O(1) time to insert each of the |V| vertices onto the front of the linked list.

15 CSC317 15 Application DFS: strongly connected components:


Download ppt "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."

Similar presentations


Ads by Google