graphs, bfs, dfs and more… ctldenon. agenda review – graphs – graphs representations – dfs...

78
Graphs, BFS, DFS and More… ctlDenon

Upload: sherilyn-wells

Post on 16-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Graphs, BFS, DFS and More…

ctlDenon

Page 2: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Agenda

• Review– Graphs – Graphs Representations– DFS– BFS

• More– Topological-Sort– Shortest Path

• Dijkstra’s , Bellman-Ford

– All Pairs Shortest Path• Dijkstra’s, Floyd-WarShall

Page 3: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Graphs

Page 4: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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 …

Page 5: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Graphs Representations - adjacency matrix

Page 6: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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??

Page 7: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Graphs Representations - adjacency list

• An adjacency list of a vertex v V is the list ∈Adj[v] of vertices adjacent to v.

• Storage size??

Page 8: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

DFS and BFS

• Review• Possible

ordering of traversing if DFS?

• Possible ordering of traversing if BFS?

Page 9: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

BFS

BFS(G)marks all Vertices in G as un-visitedmarks start vertex s as visitedenqueue s into queue Q

while queue not emptydequeue the first vertex u from queue Qfor each vertex v directly reachable from u

if v is unvisitedenqueue v to queue Qmark v as visited

Page 10: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

BFS

• Application– Shortest Path of un-weighted

while queue not emptydequeue the first vertex u from queuefor each neighbor vertex v of u

if v is unvisitedenqueue v to queuemark v as visitedparent[v] = u

Page 11: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

DFSDFS-visit(u)

color[u] = GRAYfor each neighbor vertex v of u

if color[v] = WHITEDFS-visit[v]

DFS(G)for each vertex u in G

color[u] = WHITE

for each vertex u in Gif color[u] = WHITE

DFS-visit(u)

Page 12: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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.

Page 13: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Topological SortDFS-visit(u)

color[u] = GRAYfor each neighbor vertex v of u

if color[v] = WHITEparent[v] = uDFS-visit[v]

color[u] = BLACKtime = time + 1f[u] = time

DFS(G)for each vertex u in G

color[u] = WHITEparent[u] = Nil

time = 0

for each vertex u in Gif color[u] = WHITE

DFS-visit(u)

Page 14: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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

Page 15: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

Page 16: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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.

Page 17: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Optimal substructure– Theorem. A subpath of a shortest path is a

shortest path.

Page 18: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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)

Page 19: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph?

Page 20: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS

Page 21: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights?

Page 22: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights? Dijkstra’s

algorithm

Page 23: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithmSuppose 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.

Page 24: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 25: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 26: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 27: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 28: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 29: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 30: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 31: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 32: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 33: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 34: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 35: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithm

Page 36: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Dijkstra’s algorithmvoid 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])); } } } }

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];

Page 37: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights? Dijkstra’s

algorithm

Page 38: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights? Dijkstra’s

algorithm• Negative edge weights?

Page 39: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights? Dijkstra’s

algorithm• Negative edge weights? Bellman-Ford

algorithm

Page 40: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

• If a graph G = (V, E) contains a negative-• weight cycle, then some shortest paths may

not exist.

Page 41: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, 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

Page 42: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 43: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 44: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 45: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 46: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 47: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 48: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 49: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 50: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 51: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 52: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 53: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 54: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 55: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 56: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 57: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 58: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 59: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 60: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 61: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 62: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 63: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path – Bellman Ford

Page 64: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, 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) .

Page 65: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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).

Page 66: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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

Page 67: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

Shortest Path

• Un-weighted graph? BFS• Non-negative edge weights? Dijkstra’s

algorithm• Negative edge weights? Bellman-Ford

algorithm• DAG?

Page 68: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, 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

Page 69: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, 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?

Page 70: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, 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?

Page 71: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

All Pairs Shortest Path

• Non-negative edge weights

Page 72: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

All Pairs Shortest Path

• Non-negative edge weights– Dijkstra’s algorithm |V| times

Page 73: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

All Pairs Shortest Path

• Non-negative edge weights– Dijkstra’s algorithm |V| times

• Negative edge

Page 74: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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)

Page 75: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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) .

Page 76: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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}

Page 77: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

All Pairs Shortest Path - Floyd-WarShall

Page 78: Graphs, BFS, DFS and More… ctlDenon. Agenda Review – Graphs – Graphs Representations – DFS – BFS More – Topological-Sort – Shortest Path Dijkstra’s, Bellman-Ford

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