Download presentation
1
Graphs, BFS, DFS and More…
ctlDenon
2
Agenda Review More Graphs Graphs Representations DFS BFS
Topological-Sort Shortest Path Dijkstra’s , Bellman-Ford All Pairs Shortest Path Dijkstra’s, Floyd-WarShall
3
Graphs
4
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 …
5
Graphs Representations - adjacency matrix
6
Graphs Representations - adjacency matrix
The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, n] given by A[i, j] = 1 if (i, j) ∈ E, (What if multigraph ???) 0 if (i, j) ∉ E. Storage size??
7
Graphs Representations - adjacency list
An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Storage size??
8
DFS and BFS Review Possible ordering of traversing if DFS?
Possible ordering of traversing if BFS?
9
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
10
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
11
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]
12
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.
13
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
14
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
15
Shortest Path
16
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.
17
Shortest Path Optimal substructure
Theorem. A subpath of a shortest path is a shortest path.
18
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)
19
Shortest Path Un-weighted graph?
20
Shortest Path Un-weighted graph? BFS
21
Shortest Path Un-weighted graph? BFS Non-negative edge weights?
22
Shortest Path Un-weighted graph? BFS
Non-negative edge weights? Dijkstra’s algorithm
23
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.
24
Shortest Path – Dijkstra’s algorithm
25
Shortest Path – Dijkstra’s algorithm
26
Shortest Path – Dijkstra’s algorithm
27
Shortest Path – Dijkstra’s algorithm
28
Shortest Path – Dijkstra’s algorithm
29
Shortest Path – Dijkstra’s algorithm
30
Shortest Path – Dijkstra’s algorithm
31
Shortest Path – Dijkstra’s algorithm
32
Shortest Path – Dijkstra’s algorithm
33
Shortest Path – Dijkstra’s algorithm
34
Shortest Path – Dijkstra’s algorithm
35
Shortest Path – Dijkstra’s algorithm
36
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])); }
37
Shortest Path Un-weighted graph? BFS
Non-negative edge weights? Dijkstra’s algorithm
38
Shortest Path Un-weighted graph? BFS
Non-negative edge weights? Dijkstra’s algorithm Negative edge weights?
39
Shortest Path Un-weighted graph? BFS
Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm
40
Shortest Path – Bellman Ford
If a graph G = (V, E) contains a negative- weight cycle, then some shortest paths may not exist.
41
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.
42
Shortest Path – Bellman Ford
43
Shortest Path – Bellman Ford
44
Shortest Path – Bellman Ford
45
Shortest Path – Bellman Ford
46
Shortest Path – Bellman Ford
47
Shortest Path – Bellman Ford
48
Shortest Path – Bellman Ford
49
Shortest Path – Bellman Ford
50
Shortest Path – Bellman Ford
51
Shortest Path – Bellman Ford
52
Shortest Path – Bellman Ford
53
Shortest Path – Bellman Ford
54
Shortest Path – Bellman Ford
55
Shortest Path – Bellman Ford
56
Shortest Path – Bellman Ford
57
Shortest Path – Bellman Ford
58
Shortest Path – Bellman Ford
59
Shortest Path – Bellman Ford
60
Shortest Path – Bellman Ford
61
Shortest Path – Bellman Ford
62
Shortest Path – Bellman Ford
63
Shortest Path – Bellman Ford
64
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) .
65
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).
66
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
67
Shortest Path Un-weighted graph? BFS
Non-negative edge weights? Dijkstra’s algorithm Negative edge weights? Bellman-Ford algorithm DAG?
68
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
69
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?
70
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?
71
All Pairs Shortest Path
Non-negative edge weights
72
All Pairs Shortest Path
Non-negative edge weights Dijkstra’s algorithm |V| times
73
All Pairs Shortest Path
Non-negative edge weights Dijkstra’s algorithm |V| times Negative edge
74
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)
75
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) .
76
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}
77
All Pairs Shortest Path - Floyd-WarShall
78
Reference Depth-First Search Graph Algorithms Topological Sorting Greedy Algorithms (and Graphs) Shortest Paths I Shortest Paths II Shortest Paths III
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.