graph algorithms

38
Graph Algorithms

Upload: raisie

Post on 06-Feb-2016

12 views

Category:

Documents


0 download

DESCRIPTION

Graph Algorithms. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graph Algorithms

Graph Algorithms

Page 2: Graph Algorithms

Introduction

• Terminology– V, E, directed, adjacent, path, simple path,

cycle, DAG

v1

v2

v4v5

v3

v7v8

v6

Page 3: Graph Algorithms

Introduction

• Terminology– V, E, directed, adjacent, path, simple path,

cycle, DAG

v1

v2

v4v5

v3

v7v8

v6

Page 4: Graph Algorithms

Introduction

• Terminology– connected, strongly connected, weakly

connected

v5

v7v8

v5

v7v8

Page 5: Graph Algorithms

Representation – Adjacency Matrix

v1

v2

v4v5

v3

v7v8

v6

v1 v2 v3 v4 v5 v6 v7 V8

v1

v2

v3

v4

v5

v6

V7

v8

Page 6: Graph Algorithms

Representation – Adjacency Matrix

v1

v2

v4v5

v3

v7v8

v6

v1 v2 v3 v4 v5 v6 v7 V8

v1 1 1 1

v2 1 1 1 1

v3 1 1

v4 1 1 1

v5 1 1 1 1

v6 1 1

v7 1 1 1 1

v8 1 1

Page 7: Graph Algorithms

Representation – Adjacency List

v1

v2

v4v5

v3

v7v8

v6

v2

v3

v4

v5

v6

v7

v1

v1

v2

v1

v2

v3

v1

v2

v3

v6

v2

v6

v5

v4

v4

v4

v7

v7

v5

v7

v5

v8

v8

Ø

Ø

Ø

Ø

Ø

Ø

Ø

Page 8: Graph Algorithms

Topological Ordering

• find vertex with no incoming edges

• print it

• remove it and its edges

v1

v2

v4v5

v3

v7v8

v6

Page 9: Graph Algorithms

Topological Ordering

• v1

v1

v2

v4v5

v3

v7v8

v6

v2

v4v5

v3

v7v8

v6

Page 10: Graph Algorithms

Topological Ordering

• v1, v2, v3, v7

v2

v4v5

v3

v7v8

v6

v4v5

v7v8

v6v5

v8

v6

v4v5

v3

v7v8

v6

v4

Page 11: Graph Algorithms

Topological Ordering

• v1, v2, v3, v7, v4, v5, v6, v8

• Complexity?

• Improvements?

v5

v8

v6

v8

v6v5

v8

v6v4

Page 12: Graph Algorithms

Topological Ordering

• Complexity – V2

• Improvements – as edges are removed, enqueue vertices with 0 indegree– v1, v2, v7, v3, v4, v5, v6, v8

v1

v2

v4v5

v3

v7v8

v6

Page 13: Graph Algorithms

Shortest Path Algorithms

• Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

• Unweighted – every edge has weight 1

• Applications?

Page 14: Graph Algorithms

Breadth-first Search

• Level-order traversal

Page 15: Graph Algorithms

Unweighted Shortest Path

v1

v2

v4v5

v3

v7v8

v6

s.dist = 0;for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v

Page 16: Graph Algorithms

Unweighted Shortest Path

v1

v2

v4v5

v3

v7v8

v6

enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w)

Page 17: Graph Algorithms

Unweighted Shortest Path

queue – v1

v1

v2

v4v5

v3

v7v8

v6

dv pv

v1 0 v1

v2

v3

v4

v5

v6

v7

v8

Page 18: Graph Algorithms

Unweighted Shortest Path

queue – v1

queue – v2, v4, v7

queue – v4, v7, v3, v5

queue – v7, v3, v5

queue – v3, v5, v8

queue – v5, v8, v6

v1

v2

v4v5

v3

v7v8

v6

dv pv

v1 0 v1

v2 1 v1

v3 2 v2

v4 1 v1

v5 2 v2

v6 3 v3

v7 1 v1

v8 2 v7

Page 19: Graph Algorithms

Unweighted Shortest Path

v1

v2

v4v5

v3

v7v8

v6

enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w)

s.dist = 0;for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v

Page 20: Graph Algorithms

Dijkstra’s Algorithm

• Weighted shortest-path first• Greedy algorithm

s.dist = 0for (;;) v = smallest unknown distance vertex if(v == NOT_A_VERTEX) break; v.known = true for each w adjacent to v if(!w.known) if(v.dist + cvw < w.dist) decrease(w.dist to v.dist+cvw) w.path = v

Page 21: Graph Algorithms

Dijkstra’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

known dv pv

v1 F 0 0

v2 F I 0

v3 F I 0

v4 F I 0

v5 F I 0

v6 F I 0

v7 F I 0

v8 F I 0

Page 22: Graph Algorithms

Dijkstra’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

known dv pv

v1 T 0 0

v2 F 10 v1

v3 F I 0

v4 F 7 v1

v5 F I 0

v6 F I 0

v7 F 3 v1

v8 F I 0

known dv pv

v1 T 0 0

v2 F 10 v1

v3 F I 0

v4 F 5 v7

v5 F 10 v7

v6 F I 0

v7 T 3 v1

v8 F 5 v7

Page 23: Graph Algorithms

Dijkstra’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

known dv pv

v1 T 0 0

v2 F 10 v1

v3 F I 0

v4 T 5 v7

v5 F 10 v7

v6 F I 0

v7 T 3 v1

v8 T 5 v7

known dv pv

v1 T 0 0

v2 F 10 v1

v3 F I 0

v4 T 5 v7

v5 F 10 v7

v6 F I 0

v7 T 3 v1

v8 F 5 v7

Page 24: Graph Algorithms

Dijkstra’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

known dv pv

v1 T 0 0

v2 T 10 v1

v3 F 12 0

v4 T 5 v7

v5 F 10 v7

v6 F I 0

v7 T 3 v1

v8 T 5 v7

known dv pv

v1 T 0 0

v2 T 10 v1

v3 F 12 v2

v4 T 5 v7

v5 T 10 v7

v6 F 15 v5

v7 T 3 v1

v8 T 5 v7

Page 25: Graph Algorithms

Dijkstra’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

known dv pv

v1 T 0 0

v2 T 10 v1

v3 T 12 v2

v4 T 5 v7

v5 T 10 v7

v6 T 15 v5

v7 T 3 v1

v8 T 5 v7

known dv pv

v1 T 0 0

v2 T 10 v1

v3 T 12 v2

v4 T 5 v7

v5 T 10 v7

v6 F 15 v5

v7 T 3 v1

v8 T 5 v7

Page 26: Graph Algorithms

Running time – Dijkstra’s

• Simple implementation– O(E + V2)

• Improvements– Use a priority queue– Smallest unknown distance vertex log V (V

times)– decrease log V (E times)

Page 27: Graph Algorithms

Negative Edge Costs

enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist > v.dist+cvw) w.dist = v.dist + cvw w.path = v if(w not in q) q.enqueue(w)

v1

v2

v4v5

v3

v7v8

v6

3

7

10

17

2

4

5

9

2

-6

-3

Page 28: Graph Algorithms

Network Flow

• Determine maximum flow from source to sink in a directed graph where each edge has given capacity – capacity cv,w is maximum flow for edge (v, w)

– total flow coming in must = total flow going out

• Example applications?

Page 29: Graph Algorithms

Max-Flow Algorithm

1. choose a path from src to sink – augmenting path2. add flow equal to minimum edge on path3. add reverse edges to allow algorithm to undo its

decision4. continue until no augmenting path can be found

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

5

9

2

7

src sink

Page 30: Graph Algorithms

Max-Flow Algorithm

v1

v2

v4v5

v3

v7v8

v6

10

7

3

2

16

2

4

2

9

2

4

src sink

3 3

v1

v2

v4v5

v3

v7v8

v6

87

3

2

16

2

2

2

9

2

4

src sink

3 3

22

Page 31: Graph Algorithms

Max-Flow Algorithm

v1

v2

v4v5

v3

v7v8

v6

67

3

2

1 4

2

2

5

9

2

4

src sink

3

42

2

v1

v2

v4v5

v3

v7v8

v6

3

2

5src sink

3

42

2

Page 32: Graph Algorithms

Algorithm Complexity

• O(f * E)

• Can be bad if f is large– Improve by choosing augmenting path that

increases flow by largest amount

Page 33: Graph Algorithms

Minimum Spanning Tree

• Find a tree (acyclic graph) that covers every vertex and has minimum cost– Number of edges in tree will be V-1

Page 34: Graph Algorithms

Prim’s Algorithm

• Similar to Dijkstra’s1. choose min distance vertex v and mark as

known 2. update distance values for all adjacent vertices

w1. dw = min(dw, cv, w)

v1

v2

v4v5

v3

v7v8

v6

3

7

10

17

2

4

5

9

2

6

2

Page 35: Graph Algorithms

Prim’s Algorithm

v1

v2

v4v5

v3

v7v8

v6

31

2

4

5

2

2

Page 36: Graph Algorithms

Kruskal’s Algorithm

• choose min cost edge– if it doesn’t create a cycle, add it

• use heap to provide O(ElogE) running time

v1

v2

v4v5

v3

v7v8

v6

3

7

10

17

2

4

5

9

2

6

2

Page 37: Graph Algorithms

Kruskal’s Algorithm

• (v2,v4), (v2,v3), (v4,v7), (v7,v8), (v1,v2), (v3,v6), (v1,v4), (v6,v5)

v1

v2

v4v5

v3

v7v8

v6

3

4

10

17

2

4

5

9

2

6

2

Page 38: Graph Algorithms

Depth-First Search

• Generalization of preorder traversal

dfs(Vertex v)

v.visited = true

for each w adjacent to v

if(!w.visited)

dfs(w)