Graphs, BFS, DFS and More… ctlDenon
Agenda Review More Graphs Graphs Representations DFS BFS Topological-Sort Shortest Path Dijkstra’s , Bellman-Ford All Pairs Shortest Path Dijkstra’s, Floyd-WarShall
Graphs
Graphs (review) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of a set V of vertices (singular: vertex) a set E ⊆ V × V of edges In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices. More, Connected, Disconnected Tree, Forest Cyclic, Acyclic Weighted, un-weighted Multigraph …
Graphs Representations - adjacency matrix
Graphs Representations - adjacency matrix The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by A[i, j] = 1 if (i, j) ∈ E, (What if multigraph ???) 0 if (i, j) ∉ E. Storage size??
Graphs Representations - adjacency list An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Storage size??
DFS and BFS Review Possible ordering of traversing if DFS? Possible ordering of traversing if BFS?
BFS BFS(G) marks all Vertices in G as un-visited marks start vertex s as visited enqueue s into queue Q while queue not empty dequeue the first vertex u from queue Q for each vertex v directly reachable from u if v is unvisited enqueue v to queue Q mark v as visited
BFS Application while queue not empty Shortest Path of un-weighted while queue not empty dequeue the first vertex u from queue for each neighbor vertex v of u if v is unvisited enqueue v to queue mark v as visited parent[v] = u
DFS DFS(G) for each vertex u in G color[u] = WHITE if color[u] = WHITE DFS-visit(u) DFS-visit(u) color[u] = GRAY for each neighbor vertex v of u if color[v] = WHITE DFS-visit[v]
Topological Sort Linear ordering of a directed acyclic graph (DAG)’ s nodes in which each node comes before all nodes to which it has outbound edges.
Topological Sort DFS(G) for each vertex u in G color[u] = WHITE parent[u] = Nil time = 0 if color[u] = WHITE DFS-visit(u) DFS-visit(u) color[u] = GRAY for each neighbor vertex v of u if color[v] = WHITE parent[v] = u DFS-visit[v] color[u] = BLACK time = time + 1 f[u] = time
Topological Sort Call DFS(G) to compute the finishing times f[v] for each vertex v As each vertex is finished (Mark as Black), insert it onto the front of a list Return the list
Shortest Path
Shortest Path More Formal Definition Consider a digraph G = (V, E) with edge-weight function w : E → R. The weight of path p = v1 → v2 → … → vk is defined to be A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as (u, v) = min{w(p) : p is a path from u to v}. Note: δ(u, v) = ∞ if no path from u to v exists.
Shortest Path Optimal substructure Theorem. A subpath of a shortest path is a shortest path.
Shortest Path Triangle inequality Theorem. For all u, v, x ∈ V, we have δ(u, v) ≤δ(u, x) + δ(x, v). Relaxation: if d[v] > d[x] + w(x, v) then d[v] ← d[x] + w(x, v)
Shortest Path Un-weighted graph?
Shortest Path Un-weighted graph? BFS
Shortest Path Un-weighted graph? BFS Non-negative edge weights?
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm Suppose you create a knotted web of strings, with each knot corresponding to a node, and the strings corresponding to the edges of the web: the length of each string is proportional to the weight of each edge. Now you compress the web into a small pile without making any knots or tangles in it. You then grab your starting knot and pull straight up. As new knots start to come up with the original, you can measure the straight up-down distance to these knots: this must be the shortest distance from the starting node to the destination node. The acts of "pulling up" and "measuring" must be abstracted for the computer, but the general idea of the algorithm is the same: you have two sets, one of knots that are on the table, and another of knots that are in the air. Every step of the algorithm, you take the closest knot from the table and pull it into the air, and mark it with its length. If any knots are left on the table when you're done, you mark them with the distance infinity.
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm
Shortest Path – Dijkstra’s algorithm int dist[N]; int pred[N]; struct C{ int u,c; C(){} C(int a,int b):u(a),c(b){} bool operator<(const C&a) const { return c > a.c; } }; // the graph is represented by // a adjacency list // a vertex a has adjc[a] neighbors // adj[a][0] … adj[a][adjc[a]-1] are // neighbors of vertex // cost[a][b] has edge cost for (a,b) int adjc[N]; int adj[N][N]; int cost[N][N]; bool visit[N]; void dijkstra(int s){ memset(visit,0,sizeof(visit)); memset(dist,0x7f,sizeof(dist)); priority_queue<C> pq; pq.push(C(s,0)); dist[s] = 0; while(pq.size()>0){ C c = pq.top(); pq.pop(); int u = c.u; if(visit[u]) continue; visit[u] = true; for(int i=0;i<adjc[u];++i){ int v = adj[u][i]; if(visit[v]) continue; if(c.c+cost[u][v]<dist[v]){ dist[v] = dist[u] + cost[u][v]; pred[v] = u; pq.push(C(v, dist[v])); }
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights?
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm
Shortest Path – Bellman Ford If a graph G = (V, E) contains a negative- weight cycle, then some shortest paths may not exist.
Shortest Path – Bellman Ford Bellman-Ford algorithm: Finds all shortest-path lengths from a sources∈V to all v∈V or determines that a negative-weight cycle exists.
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford
Shortest Path – Bellman Ford Theorem. If G= (V, E)contains no negative-weight cycles, then after the Bellman-Ford algorithm executes, d[v] = δ(s, v)for all v∈V. Proof. Let v∈V be any vertex, and consider a shortest path p from s to v with the minimum number of edges. Since p is a shortest path, we have δ(s, vi) = δ(s, vi–1) + w(vi–1, vi) .
Shortest Path – Bellman Ford If G contains no negative-weight cycles, p is simple. Longest simple path has ≤|V|–1edges. Initially, d[v0] = 0 = δ(s, v0), and d[v0] is unchanged by subsequent relaxations (because of the lemma from Lecture 14 that d[v] ≥δ(s, v)). After 1pass through E, we have d[v1] = δ(s, v1). After 2passes through E, we have d[v2] = δ(s, v2).M After k - passes through E, we have d[vk] = δ(s, vk).
Shortest Path – Bellman Ford If a value d[v] fails to converge (stop changing) after |V|–1passes, there exists a negative-weight cycle in G reachable from s
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG?
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford All Pairs Shortest Path?
Shortest Path Un-weighted graph? BFS Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG? Topological Sort + One Round of Bellman-Ford All Pairs Shortest Path?
All Pairs Shortest Path Non-negative edge weights
All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times
All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times Negative edge
All Pairs Shortest Path Non-negative edge weights Dijkstra’s algorithm |V| times Negative edge Floyd-WarShall (Not the fastest one, but good enough in most cases)
All Pairs Shortest Path - Floyd-WarShall Define Cij(k)=weight of a shortest path from I to j with intermediate vertices belonging to the set {1, 2, …, k}. Thus, δ(i, j) = Cij(n). Also, Cij(0) = w(i, j) .
All Pairs Shortest Path - Floyd-WarShall Cij(k)= min { Cij(k–1),Cik(k–1)+ C kj(k–1) } intermediate vertices in {1, 2, …, k}
All Pairs Shortest Path - Floyd-WarShall
Reference Depth-First Search http://en.wikipedia.org/wiki/Depth-first_search Graph Algorithms http://i.cs.hku.hk/~provinci/training07/02-Graph_Theory.ppt Topological Sorting http://en.wikipedia.org/wiki/Topological_sorting Greedy Algorithms (and Graphs) http://velblod.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_leiserson_lec16/mit6046jf05_leiserson_lec16_01.pdf Shortest Paths I http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec17/mit6046jf05_demaine_lec17_01.pdf Shortest Paths II http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec18/mit6046jf05_demaine_lec18_01.pdf Shortest Paths III http://carbon.videolectures.net/2005/ocw/mit/6.046j/mit6046jf05_demaine_lec18/mit6046jf05_demaine_lec19_01.pdf