Download presentation
Presentation is loading. Please wait.
1
CSC317 Graph algorithms Why bother?
Fundamental to many problems: Web graphs. Biology. Other. Connectivity – is network connected and can you get from one node to another, or what is the shortest path? Examples: Driving directions get to one contact through another social media or contact path. CSC317
2
Notation: We’ll usually describe the vertices (T) and edges (E) of graph G=(V, E). We sometimes talk about vertices v in V, and edges e in E. We denote the number of vertices and edges as n and m respectively. (The book sometimes also uses V and E to denote number of vertices and edges). 2 forms of representation: (1) Adjacency list: each vertex has a linked list to each adjacent vertex (can save in space O(n + m) slower lookup good in the case of a relatively sparse graph) (2) Adjacency Matrix (takes up more space O(n2), faster lookup good in the case of a dense graph) Note: number of edges can be between O(1) and O(n squared), depending on whether graph is sparse or dense CSC317
3
CSC317
4
Great! How do we find vertices that can be reached from a given vertex (node)?
Two ways to search graphs: BFS DFS Breadth First Search (BFS): Concept: Find all vertices distance 1 then distance 2 then distance 3 etc... by breadth. Like visiting according to levels (of distance 1 then 2 then 3 etc). directed undirected CSC317
5
Application: finding shortest paths between vertices (eg, from source vertex s to all other vertices). Run time: we will see that BFS is fast and linear in number of vertices and nodes: O(n + m) Okay. So, how are we going to do the breadth first search? - We color the nodes to keep track of them (in BFS, DFS): White: Vertices are initially white Gray: First time vertex encountered, make gray Black: Only when all its adjacent neighbors discovered, turn to black. Main approach: Uses first in first out queue Q (FIFO) to manage the discovered gray vertices, until we go through all their adjacency neighbors, and they then turn black. Same for directed and undirected graphs. CSC317
6
CSC317 G is a graph and s is some source vertex
initialize all vertices to white, and distance infinity initially make s vertex gray, distance 0, empty Q and Enqueue(Q, s) get vertex u from Q loop over all nodes v that are connected to u first discovered, put in the back of queue v colored gray v distance update, predecessor of v is u, v back in Q u colored black CSC317
7
CSC317
8
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) CSC317
9
CSC317 Why are we doing this? BFS allows us to find shortest paths!
Shortest path distance from s to v: d(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 d(s,u)=∞ and the inequality holds. CSC317
10
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 CSC317
11
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! CSC317
12
CSC317 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). CSC317
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.