1 dijkstra’s minimum-path algorithm minimum spanning tree cse 30331 lectures 20 – intro to...

14
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

Upload: gwen-richardson

Post on 16-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

1

Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree

CSE 30331Lectures 20 – Intro to Graphs

Page 2: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

2

Minimum (weight) path – Dijkstra’s algorithm Uses priority queue containing identities of all fringe vertices and the

length of the minimum path to each from the start Algorithm builds a tree of all minimum length paths from start Each vertex is either tree, fringe or unseen

At each step The fringe vertex V with the minimum path is removed from priorityQ and added to the tree V’s non-tree neighbors U become fringe and the minimum path length is computed from start, thru V to U and is stored in U.dataValue, V is saved as U.parent and U is added to priorityQ

Process stops when queue is empty, or chosen destination vertex is found

Page 3: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

3

Dijkstra Minimum-Path Algorithm (Example A to D)

A B

CD

E

F

6

4

2 0 1 4

12

4

2 6

8 11

PriQ: (A,0) Tree (vertices & path weight)(B,4) (C,11) (E,4) A,0(E,4) (C,11) (C,10) (D,12) A,0 B,4(C,10) (C,11) (D,12) A,0 B,4 E,4(C,11) (D,12) A,0 B,4 E,4 C,10(D,12) A,0 B,4 E,4 C,10empty A,0 B,4 E,4 C,10 D,12

Page 4: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

4

Minimum Spanning TreePrim’s Algorithm Spanning tree for graph with minimum TOTAL weight Minimum Spanning Tree may not be unique, but total weight is

same value for all All vertices are either tree, fringe, or unseen Priority queue is used to hold fringe vertices and the minimum

weight edge connecting each to the tree

Put start vertex in priorityQWhile priorityQ not empty The nearest vertex V is removed from the queue and added to the tree For each non-tree neighbor U of V if the edge V,U weight < current U.dataValue U.dataValue is set to weight of edge V,U U.parent is set to V push U:weight pair onto priority queue

Page 5: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

5

Minimum Spanning Tree Example

B

A

H

E

G

F

D

C

5 0 2 5

4 62 5

2 3

5 5

3 2

9 8

3 5

6 7

B

A

H

E

G

F

D

C

2 5

4 6

2 52 3

5 5

3 2

3 5

M in im u m am o u n t o f cab le = 2 4 1

M in im u m s p an n in g t reeN et w o rk o f H u b s

Page 6: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

6

Minimum Spanning Tree: Step 1 (edge A-B)

A

B C

D

2

8

12

5

7

A

B

2

Spanning tree with vertices A, B

minSpanTreeSize = 2, minTreeWeight = 2

Page 7: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

7

Minimum Spanning Tree:Step 2 (Edge A-D)

A

B C

D

2

8

12

5

7

D

A

B

Spanning tree with vertices A, B, DminSpanTreeSize = 3, minTreeWeight = 7

2

5

Page 8: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

8

Minimum Spanning Tree:Step 3 (Edge D-C)

A

B C

D

2

8

12

5

7

C

7D

A

B

Spanning tree with vertices A, B, D, CminSpanTreeSize = 4, minTreeWeight = 14

2

5

Page 9: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

9

Runtime Orders of Complexity

Min Spanning Tree – O(V + E log2E)

Min Path (Dijkstra) – O(V + E log2E)

Strong Components – O(V + E)

Dfs – O(V+E)

BFS – O(V+E)

Page 10: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

10

Graphs – Important Terms

Vertex, edge, adjacency, path, cycle Directed (digraph), undirected Complete Connected (strongly, weakly, components) Searches (DFS, BFS) Shortest Path, Minimum Path Euler Path, Hamiltonian Path Minimum Spanning Tree

Page 11: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

11

Searching Graphs

Breadth-First Search, bfs() Locates all vertices reachable from a starting

vertex Uses a queue in process Can be used to find the minimum distance from a

starting vertex to an ending vertex in a graph.

Page 12: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

12

Searching Graphs

Depth-First Search, dfs() Produces a list of all graph vertices in the reverse

order of their finishing times. Supported by a recursive depth-first visit function,

dfsVisit() An algorithm can check to see whether a graph is

acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG)

Forms the basis for an efficient algorithm that finds the strong components of a graph

Page 13: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

13

Searching Graphs

Dijkstra's algorithm (minimum path) Uses a priority queue to determine a path from a

starting to an ending vertex, of minimum weight Prim's algorithm (minimum spanning tree)

An extension of Dijkstra’s algorithm, which computes the minimum spanning tree of an undirected, connected graph.

Page 14: 1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE 30331 Lectures 20 – Intro to Graphs

14

Alaska Road Map

Assignment File format Demo