67
Algorithms analysis and design BY Lecturer: Aisha Dawood

Algorithms analysis and design

  • Upload
    xia

  • View
    80

  • Download
    0

Embed Size (px)

DESCRIPTION

Algorithms analysis and design. BY Lecturer: Aisha Dawood. Graph algorithms. MST Prim Kruskal Dijkstra. Minimum Spanning Tree. Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight. 6. 4. 5. 9. 14. 2. 10. 15. 3. - PowerPoint PPT Presentation

Citation preview

Page 1: Algorithms analysis and design

Algorithms analysis and design

BYLecturer: Aisha Dawood

Page 2: Algorithms analysis and design

Graph algorithms

● MST● Prim● Kruskal● Dijkstra

Page 3: Algorithms analysis and design

Minimum Spanning Tree

● Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 45

2

9

15

8

Page 4: Algorithms analysis and design

Minimum Spanning Tree

● Which edges form the minimum spanning tree (MST) of the below graph?

H B C

G E D

F

A

1410

3

6 45

2

9

15

8

Page 5: Algorithms analysis and design

Minimum Spanning Tree

● Answer:

H B C

G E D

F

A

1410

3

6 45

2

9

15

8

Page 6: Algorithms analysis and design

Minimum Spanning Tree

● MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees■ Let T be an MST of G with an edge (u,v) in the middle■ Removing (u,v) partitions T into two trees T1 and T2

■ T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2)

■ w(T) = w(u,v) + w(T1) + w(T2)

Page 7: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Page 8: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14 10

3

6 45

2

9

15

8

Run on example graph

Page 9: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14 10

3

6 45

2

9

15

8

Run on example graph

Page 10: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

0

14 10

3

6 45

2

9

15

8

Pick a start vertex r

r

Page 11: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

0

14 10

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

Page 12: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

0

3

14 10

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

Page 13: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14

0

3

14 10

3

6 45

2

9

15

8

u

Page 14: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14

0

3

14 10

3

6 45

2

9

15

8u

Page 15: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

14

0 8

3

14 10

3

6 45

2

9

15

8u

Page 16: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10

0 8

3

14 10

3

6 45

2

9

15

8u

Page 17: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10

0 8

3

14 10

3

6 45

2

9

15

8u

Page 18: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10 2

0 8

3

14 10

3

6 45

2

9

15

8u

Page 19: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10 2

0 8 15

3

14 10

3

6 45

2

9

15

8u

Page 20: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10 2

0 8 15

3

14 10

3

6 45

2

9

15

8

u

Page 21: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10 2 9

0 8 15

3

14 10

3

6 45

2

9

15

8

u

Page 22: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

10 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 23: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 24: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 25: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 26: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 27: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

5 2 9

0 8 15

3

4

14 10

3

6 45

2

9

15

8

u

Page 28: Algorithms analysis and design

Prim’s Algorithm

MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Total running time = O(V+V2+V.E) = O(V2), if extractmin implemented as a queue we can achieve O(V log V) if it is implemented as a heap.

Page 29: Algorithms analysis and design
Page 30: Algorithms analysis and design

Single-Source Shortest Path

● Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges■ E.g., a road map: what is the shortest path from

Chapel Hill to Charlottesville?

Page 31: Algorithms analysis and design

Shortest Path Properties

● Again, we have optimal substructure: the shortest path consists of shortest subpaths:

Page 32: Algorithms analysis and design

Shortest Path Properties

● Define (u,v) to be the weight of the shortest path from u to v

● Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v)

x

u v

This path is no longer than any other path

Page 33: Algorithms analysis and design

Relaxation

● A key technique in shortest path algorithms is relaxation■ Idea: for all v, maintain upper bound d[v] on (s,v)Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w;}

95 2

75 2

Relax

65 2

65 2

Relax

Page 34: Algorithms analysis and design

Dijkstra’s Algorithm

● Similar to breadth-first search■ Grow a tree gradually, advancing from vertices

taken from a queue● Also similar to Prim’s algorithm for MST

■ Use a priority queue keyed on d[v]

Page 35: Algorithms analysis and design

Dijkstra’s Algorithm

Dijkstra(G) for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

enqueue(S,u); for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);

RelaxationStep

B

C

DA

10

4 3

2

15

Page 36: Algorithms analysis and design

Dijkstra’s Algorithm

Dijkstra(G) for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);

enqueue(S,u); for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);

O(V)

O(V2) (since we have to search through the entire array)

O(V.E)

Total running time = O(V+V2+V.E) = O(V2), if extractmin implemented as a queue we can achieve O(V log V) if it is implemented as a heap.

Page 37: Algorithms analysis and design

Dijkstra’s Algorithm

Page 38: Algorithms analysis and design

Dijkstra’s Algorithm

Page 39: Algorithms analysis and design

Kruskal’s Algorithm

● create a set E containing all the edges in the graph

● while E is nonempty and the tree is not yet spanning■ remove an edge with minimum weight from E■ if that edge connects two different trees, then add it

to the forest, combining two trees into a single tree■ otherwise discard that edge.

Page 40: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

Page 41: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 42: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 43: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 44: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1?

5

13

1725

148

21

Run the algorithm:

Page 45: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 46: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2? 199

1

5

13

1725

148

21

Run the algorithm:

Page 47: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 48: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5?

13

1725

148

21

Run the algorithm:

Page 49: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 50: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148?

21

Run the algorithm:

Page 51: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 52: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199?

1

5

13

1725

148

21

Run the algorithm:

Page 53: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 54: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13?

1725

148

21

Run the algorithm:

Page 55: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 56: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

14?8

21

Run the algorithm:

Page 57: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 58: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

17?25

148

21

Run the algorithm:

Page 59: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 19?9

1

5

13

1725

148

21

Run the algorithm:

Page 60: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21?

Run the algorithm:

Page 61: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725?

148

21

Run the algorithm:

Page 62: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 63: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

2 199

1

5

13

1725

148

21

Run the algorithm:

Page 64: Algorithms analysis and design

Kruskal’s Algorithm

Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}

What will affect the running time? 1 Sort

O(V) MakeSet() callsO(E) FindSet() callsO(V) Union() calls

Page 65: Algorithms analysis and design

Kruskal’s Algorithm

Page 66: Algorithms analysis and design

Kruskal’s Algorithm: Running Time

● To summarize: ■ Sort edges: O(E lg E), merge sort.■ O(V) MakeSet()’s■ O(E) FindSet()’s■ O(V) Union()’s ■ Total running time O(E lg E), almost linear

without sorting

Page 67: Algorithms analysis and design

Disjoint Set Union: Analysis

● Union(A,B): “copy” elements of A into B: ??? time

● Worst-case analysis: O(n2) time for n Union’sUnion(S1, S2) “copy” 1 elementUnion(S2, S3) “copy” 2 elements…Union(Sn-1, Sn) “copy” n-1 elements

O(n2)

● Improvement: always copy smaller into larger.