1 Introduction to Graph Data Structure Applications Graph Searching Minimum Spanning Trees ...

Preview:

Citation preview

1

Introduction to Graph Data Structure

Applications

Graph Searching

Minimum Spanning Trees

Shortest Path problems

2

Graph Definitions A graph G is denoted by G = (V, E) where

◦ V is the set of vertices or nodes of the graph◦ E is the set of edges or arcs connecting the vertices in V

Each edge E is denoted as a pair (v,w) where v,w e V

For example in the graph below

1

2

5

6 4

3

V = {1, 2, 3, 4, 5, 6}E = {(1, 2) (2, 5) (3, 6) (4, 6) (5, 6)}● This is an example of an

unordered or undirected graph

3

Graph Definitions

Complete Graph◦ A graph is complete, if every possible edge exists

4

Graph Definitions (contd.) If the pair of vertices is ordered then the

graph is a directed graph or a di-graph

1

2

5

6 4

3 Here, V = {1, 2, 3, 4, 5, 6}E = {(1, 2) (2, 5) (5, 6) (6, 3) (6, 4)}

● Vertex v is adjacent to w iff (v,w) e E● Sometimes an edge has another component called a

weight or cost. If the weight is absent it is assumed to be 1

5

Graph Definitions (contd.)

1

2

5

6 4

3

1.2

2

1

2.5

4

Here, V = {1, 2, 3, 4, 5, 6}E = {(1, 2,4) (2, 5,1) (5,

6,1.2) (6, 3,2.5) (6, 4,2)}

6

Path

A path of length k is a sequence v , v , …, v of vertices such that (v , v ) for i = 0, 1, …, k – 1 is an edge of G.

0 1 k

i i+1

g

a

e

j

n

b

f

k

dc

o

h

l

p

m

q

Non-simple path:

a, b, e, f, g, b, g, l

Simple path:a, e, k, p, l, qm, h, d, c, g

(no repeatedvertices)

b, c, d not a path

g

a

e

j

n

b

f

k

dc

o

h

l

p

m

q

g

a

e

j

n

b

f

k

dc

o

h

l

p

m

q

ff

b

f ge

j

n

f

k

d

o

h

l

p

m

q

fff

c

ge

j

n

f

k

d

o

h

l

p

m

q

fff

b c

ge

j

n

f

k

d

o

h

l

p

m

q

fff

a b c

ge

j

n

f

k

d

o

h

l

p

m

q

fff

7

Cycle

a

e

j

n

b

f

k

dc

o

g h

l

p

m

q

A cycle is a path that starts and ends at the same vertex.

A simple cycle has no repeated vertices.

k, j, n, k, p, o,k is not simple.

jj

n

j

n

j

n

j

n

j

on

j

8

Subgraph

a

e

j

n

b

f

k

dc

o

g h

l

p

m

q

A subgraph H of G

is a graph; its edges and vertices are subsets of those of G.

V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b, e), (b, g), (e, f), (d, h), (l, p), (l, q)}

9

ConnectivityG is connected if there is a path between every pair of vertices.

a

b

d

fe

c

If G is not connected, the maximal connected subgraphs are the connected components of G.

e f ga

cb

dC

CC

1

32

10

Strong & Weak ConnectivityA directed graph is strongly connected if every two vertices are reachable from each other.

a

f

e

cd

b

It is weakly connected if the underlying undirected graph is connected.

f

e

a

d

c

b

Tree is a graph in which any two vertices are connected by exactly one path

or any connected

graph with no cycles is a tree.

A forest is a disjoint union of trees.

11

Tree, Forest

12

Property of ConnectivityLet G = (V, E) be an undirected graph.

If G is connected, then | E | ≥ | V | – 1.

If G is a tree, then | E | = | V | – 1.

If G is a forest, then | E | ≤ | V | – 1.

13

Degree In an undirected graph, the degree of a

vertex is the number of edges that are attached to that vertex.

Each vertex has degree = 3

14

Degree cont.

In a directed graph, we have◦ In-degree: # of edges that end at that vertex◦ Out-degree: # of edges that originate at that

vertex

1

2

5

4

3

2

31

2

3

4

1

2

3

4

1

2

3

6

15

Applications of Graphs Driving Map

◦ Edge = Road◦ Vertex = Intersection ◦ Edge weight = Time required to cover the road

Airline Traffic◦ Vertex = Cities serviced by the airline◦ Edge = Flight exists between two cities◦ Edge weight = Flight time or flight cost or both

Computer networks◦ Vertex = Server nodes◦ Edge = Data link◦ Edge weight = Connection speed

Applications of Graphs

16

Graphs describe relationships

Molecules

Social Networks

Geometric Surfaces (CAD)

Circuits

Parts in an Assembly

JohnYoko Ringo

George

Paul

Linda

Flow Charts

17

Representing Graphs: Adjacency Matrix

Adjacency Matrix ◦ Two dimensional matrix of size n x n where n is the number

of vertices in the graph

◦ a[i, j] = 0 if there is no edge between vertices i and j

◦ a[i, j] = 1 if there is an edge between vertices i and j

◦ Undirected graphs have both a[i, j] and a[j, i] = 1 if there is an edge between vertices i and j

For weighted graphs, a[i,j] = weight of edge between vertices i and j

18

Adjacency Matrix

2

1 3

5 4

ijA = (a )

a = ij

1 if (i, j) E(G)

0 otherwise

1 0 1 1 0 12 1 0 1 0 03 1 1 0 1 14 0 0 1 0 15 1 0 1 1 0

1 2 3 4 5

Space: (|V| ). 2

Preferred when the graph is small or dense.

19

Graphs: Adjacency Matrix Example:

1

2 4

3

a

d

b c

A 1 2 3 4

1

2

3 ??4

Symmetric or un-symmetric?

20

Graphs: Adjacency Matrix Example:

1

2 4

3

a

d

b c

A 1 2 3 4

1 0 1 1 0

2 0 0 1 0

3 0 0 0 0

4 0 0 1 0

21

Problem with Adjacency Matrix Space requirement is Q(N2)

Problem: The array is very sparsely populated. For example if a directed graph has 4 vertices and 3 edges, the adjacency matrix has 16 cells only 3 of which are 1

22

Adjacency Lists

2

1 3

5 4

2 3 5

1 3

1 2 4 5

3 5

1 3 4

If G is directed, the total length of all the adjacency lists is | E |.

1

2

3

4

5

Adj

If G is undirected, the total length is 2 | E |.

Space requirement: (|V| + |E|). Preferable representation.

23

Tradeoffs between Adjacency Matrices & Adjacency Lists

Comparison Winner

Faster to test if (x, y) exists ? Matrices

Faster to find vertex degree ? Lists

Less memory on small graphs ? Lists (m+n) vs. Matrices O(n2)

Edge insertion or deletion ? Matrices (1) vs. Lists O(di)

Faster to traverse the graph ? Lists (m+n) vs. Matrices (n2)

Better for most Problems ? Lists

24

Negative Cost Cycle A negative cost cycle is a cycle such that the sum

of the costs of the edges is negative The more we cycle through a negative cost cycle,

the lower the cost of the cycle becomes

2

3

4

4 2

-12

Cost of the path v1-v5

• No traversal of cycle: 6 • One traversal of cycle: 0• Two traversals of cycle: -6• Three traversals of cycle: -12

...

5

1

5

1

• Negative cost cycles are not allowed when we traverse a graph to find the weighted shortest path

25

Using a model to solve a complicated traffic light problem

GIVEN: A complex intersection.

OBJECTIVE: Traffic light with minimum phases.

SOLUTION:• Identify permitted turns, going straight is a “turn”.

• Make group of permitted turns.

• Make the smallest possible number of groups.

• Assign each phase of the traffic light to a group.

26

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

27

Using a model to solve a complicated traffic light problem

Roads C and E are one way, others are two way.

There are 13 permitted turns.

Some turns such as AB (from A to B) and EC can be carried out simultaneously.

Other like AD and EB cross each other and can not be carried out simultaneously.

The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.

28

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

AB & AC

AD & EB

29

Using a model to solve a complicated traffic light problem

SOLUTION:

• We model the problem using a structure called graph G(V,E).

• A graph consists of a set of points called vertices, and lines connecting the points, called edges.

• Drawing a graph such that the vertices represent turns.

• Edges between those turns that can NOT be performed simultaneously.

30

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

AB AC AD

BA BC BD

DA DB DC

EA EB EC ED

Partial graph of incompatible turns

31

Using a model to solve a complicated traffic light problem

Partial table of incompatible turns

32

Using a model to solve a complicated traffic light problem

SOLUTION:• The graph can aid in solving our problem.

• A coloring of a graph is an assignment of a color to each vertex of the graph, so that no two vertices connected by an edge have the same color.

• Our problem is of coloring the graph of incompatible turns using as few colors as possible.

33

Using a model to solve a complicated traffic light problem

GIVEN: A complex intersection.

OBJECTIVE: Traffic light with minimum phases.

SOLUTION:• Identify permitted turns, going straight is a “turn”.

• Make group of permitted turns.

• Make the smallest possible number of groups.

• Assign each phase of the traffic light to a group.

34

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

35

Using a model to solve a complicated traffic light problem

Roads C and E are one way, others are two way.

There are 13 permitted turns.

Some turns such as AB (from A to B) and EC can be carried out simultaneously.

Other like AD and EB cross each other and can not be carried out simultaneously.

The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.

36

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

AB & AC

AD & EB

37

Using a model to solve a complicated traffic light problem

SOLUTION:

• We model the problem using a structure called graph G(V,E).

• A graph consists of a set of points called vertices, and lines connecting the points, called edges.

• Drawing a graph such that the vertices represent turns.

• Edges between those turns that can NOT be performed simultaneously.

38

Using a model to solve a complicated traffic light problem

AB

C

D

E

An intersection

AB AC AD

BA BC BD

DA DB DC

EA EB EC ED

Partial graph of incompatible turns

39

Using a model to solve a complicated traffic light problem

Partial table of incompatible turns

40

Using a model to solve a complicated traffic light problem

SOLUTION:• The graph can aid in solving our problem.

• A coloring of a graph is an assignment of a color to each vertex of the graph, so that no two vertices connected by an edge have the same color.

• Our problem is of coloring the graph of incompatible turns using as few colors as possible.

41

Graphs

Muhammad Amjad Iqbal

42

Introduction to Graph Data Structure

Applications

Graph Searching

Minimum Spanning Trees

Shortest Path problems

43

Graphs Searching

44

Graph Searching

Given: a graph G = (V, E), directed or undirected

Goal: methodically explore every vertex and every edge

Ultimately: build a tree on the graph◦ Pick a vertex as the root◦ Choose certain edges to produce a tree◦ Note: might also build a forest if graph is not

connected

45

Breadth-First Search Given a G=(V,E) and distinguished source vertex s,

BFS systematically explores the edges of G to “discover” every vertex reachable from s.

Creates a BFS tree rooted at s that contains all such vertices.

Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier.

The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1

46

will associate vertex “colors” to guide the algorithm◦ White vertices have not been discovered All vertices start out white

◦ Grey vertices are discovered but not fully explored They may be adjacent to white vertices and represent the

frontier between the discovered and the undiscovered.◦ Black vertices are discovered and fully explored They are adjacent only to black and gray vertices

Explore vertices by scanning adjacency list of grey vertices

Breadth-First Search

47

BFS(G, s) { for each vertex u V [G] - {s} do color[u] ← WHITE u->d ← ∞ u->p ← NIL Q = {s}; // Q is a queue initialize to s while (Q not empty) { u = Dequeue(Q); for each v u->adj { if (v->color == WHITE)

{ v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v);

} } u->color = BLACK; }}

Breadth-First Search

What does v->p represent?

What does v->d represent?

48

Breadth-First Search: Example

r s t u

v w x y

49

Breadth-First Search: Example

0

r s t u

v w x y

sQ:

50

Breadth-First Search: Example

1

0

1

r s t u

v w x y

wQ: r

51

Breadth-First Search: Example

1

0

1

2

2

r s t u

v w x y

rQ: t x

52

Breadth-First Search: Example

1

2

0

1

2

2

r s t u

v w x y

Q: t x v

53

Breadth-First Search: Example

1

2

0

1

2

2

3

r s t u

v w x y

Q: x v u

54

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: v u y

55

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: u y

56

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: y

57

Breadth-First Search: Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: Ø

58

Breadth-First Search: Properties BFS calculates the shortest-path distance to

the source node◦ Shortest-path distance (s,v) = minimum number

of edges from s to v, or if v not reachable from s BFS builds breadth-first tree, in which paths

to root represent shortest paths in G◦ Thus can use BFS to calculate shortest path from

one vertex to another in O(V+E) time

59

Depth First SearchDepth-first search: StrategyGo as deep as can visiting un-visited nodesChoose any un-visited vertex when you have a

choiceWhen stuck at a dead-end, backtrack as little aspossibleBack up to where you could go to another unvisited

vertexThen continue to go on from that pointEventually you’ll return to where you started

60

DFS AlgorithmDFS(G)

for each vertex u V[G] {color[u]=whiteparent[u]=NULL

}time=0 for each vertex u V[G] {

if color[u]=white thenDFS-VISIT(u)

}

61

DFS-VISIT(u)color[u]=GRAYtime=time+1d[u]=time for each vertex v adj[u] {

if color[v]=white Thenparent[v]=uDFS-VISIT(v)

}color[u]=blackf[u]=time=time+1

62

Depth-First Search: Example

u v w

x y z

63

Depth-First Search: Example

1/

u v w

x y z

64

Depth-First Search: Example

1/ 2/

u v w

x y z

65

Depth-First Search: Example

1/ 2/

3/

u v w

x y z

66

Depth-First Search: Example

1/

4/

2/

3/

u v w

x y z

67

Depth-First Search: Example

1/

4/

2/

3/

u v w

x y z

B

68

Depth-First Search: Example

1/

4/5

2/

3/

u v w

x y z

B

69

Depth-First Search: Example

1/

4/5

2/

3/6

u v w

x y z

B

70

Depth-First Search: Example

1/

4/5

2/7

3/6

u v w

x y z

B

71

Depth-First Search: Example

1/

4/5

2/7

3/6

u v w

x y z

BF

72

Depth-First Search: Example

1/8

4/5

2/7

3/6

u v w

x y z

BF

73

Depth-First Search: Example

1/8

4/5

2/7

3/6

9/

u v w

x y z

BFC

74

Depth-First Search: Example

1/8

4/5

2/7

3/6

9/

10/

u v w

x y z

BFC

B

75

Depth-First Search: Example

1/8

4/5

2/7

3/6

9/

10/11

u v w

x y z

BFC

B

76

Depth-First Search: Example

1/8

4/5

2/7

3/6

9/12

10/11

u v w

x y z

BFC

B

Minimum Spanning Tree (MST)

Motivation

For an electrical circuit certain pins have to be grounded. Finding the arrangement of wires (connecting those pins) that uses the least amount of wire.

Let a G(V,E) be a graph such that (u, v) E and a weight w(u,v) corresponding to wire needed to join u and v.

We are looking for an acyclic subset T E that connects all vertices and whose total weight w(T) is minimized.

TvuvuwTw

),(),()(

Motivation

Since T is acyclic and connects all the vertices, it must form a tree.

Since it spans the graph, it is called a spanning tree.

MST or actually means Minimum Weight Spanning Tree.

Definitions

The Spanning Tree◦ Subset of edges that forms a tree (connected and

acyclic), and includes every vertex Minimum Spanning Tree

◦ Spanning tree having minimum total weight on its edges

TvuvuwTw

),(),()(

Example: A Graph

Spanning tree: Cyclic

Spanning Tree: Acyclic

Is this minimum spanning tree

84

Finding Minimum Spanning Tree

Let C1 be the starting nodecompare weights of all arcs joining C1 to rest of the nodeschoose the smallest weight

arc

85

Finding Minimum Spanning Tree

Add node 2 to the spanningtree determined so far.

86

Finding Minimum Spanning Tree

Add node 2 to the spanning tree

determined so far. Compareweights of all arcs joining the spanning tree to rest of the

nodeschoose the smallest weight

arcAdd node 5 to the spanning

treedetermined so far.

87

Finding Minimum Spanning Tree

Minimum Spanning Tree

Problem: given a connected, undirected, weighted graph:

1410

3

6 4

5

2

9

15

8

Minimum Spanning Tree Problem: given a connected, undirected,

weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 4

5

2

9

15

8

Minimum Spanning Tree

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

H B C

G E D

F

A

1410

3

6 4

5

2

9

15

8

Answer:

Minimum Spanning Tree

H B C

G E D

F

A

1410

3

6 4

5

2

9

15

8

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

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

Prim’s Algorithm

1410

3

6 45

2

9

15

8

Run on example graph

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

Prim’s Algorithm

1410

3

6 45

2

9

15

8

Run on example graph

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

Prim’s Algorithm

0

1410

3

6 45

2

9

15

8

Pick a start vertex r

r

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

Prim’s Algorithm

0

1410

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

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

Prim’s Algorithm

0

3

1410

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

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

Prim’s Algorithm

14

0

3

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

14

0

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

14

0 8

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

10

0 8

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

10

0 8

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

10 2

0 8

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

10 2

0 8 15

3

1410

3

6 45

2

9

15

8u

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

Prim’s Algorithm

10 2

0 8 15

3

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

10 2

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Prim’s Algorithm

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

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

Review: Prim’s Algorithm

What is the hidden cost in this code?

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;

Review: Prim’s Algorithm

How often is ExtractMin() called?

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

Review: Prim’s Algorithm

What will be the running time?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

MST-KRUSKAL(G,w)A=NULLfor each vertex v V[G]

Make-Set(v)sort the edges(E) in increasing order by weight wfor each edge (u,v) E

if FindSet(u) FindSet(v)A=A U {(u,v)}UNUION(u,v)

Kruskal’s Algo for MST

1410

3

6 45

2

9

15

8

Kruskal’s Algorithm

Graph Optimization Problems and

Greedy AlgorithmsGreedy Algorithms

// Make the best choice now!

Optimization ProblemsMinimizing Cost or Maximizing BenefitsMinimum Spanning Tree

Minimum cost for connecting all verticesSingle-Source Shortest Paths

Shortest Path between two vertices

Greedy Algorithms: Make the best choice now! Making choices in sequence such that

◦ each individual choice is best according to some limited “short-term” criterion, that is not too expensive to evaluate

◦ once a choice is made, it cannot be undone! even if it becomes evident later that it was a poor choice

Make progress by choosing an action that◦ incurs the minimum short-term cost,◦ in the hope that a lot of small short-term costs

add up to small overall cost. Possible drawback:

◦ actions with a small short-term cost may lead to a situation, where further large costs are unavoidable.

Optimization Problems Minimizing the total cost or

Maximizing the total benefits◦ Analyze all possible outcomes and find the best,

or◦ Make a series of choices whose overall effect is to

achieve the optimal. Some optimization problems can be solved

exactly by greedy algorithms◦ Minimum cost for connecting all vertices Minimum Spanning Tree Algorithm

◦ Shortest Path between two vertices Single-Source Shortest Paths Algorithm

Minimum Spanning Tree A spanning tree for a connected, undirected

graph, G=(V,E) is ◦ a subgraph of G that is ◦ an undirected tree and contains ◦ all the vertices of G.

In a weighted graph G=(V,E,W), the weight of a subgraph is ◦ the sum of the weights of the edges in the subgraph.

A minimum spanning tree for a weighted graph is ◦ a spanning tree with the minimum weight.

Prim’s Minimum Spanning Tree Algorithm Select an arbitrary starting vertex, (the root) branches out from the tree constructed so far by

◦ choosing an edge at each iteration◦ attach the edge to the tree that edge has minimum weight among all edges that can be

attached◦ add to the tree the vertex associated with the edge

During the course of the algorithm, vertices are divided into three disjoint categories:◦ Tree vertices: in the tree constructed so far,◦ Fringe vertices: not in the tree, but adjacent to some

vertex in the tree,◦ Unseen vertices: all others

The Algorithm in action, e.g.

Prim’s Minimum Spanning Trees: Outline

Properties of Minimum Spanning Trees Definition: Minimum spanning tree property

◦ Let a connected,weighted graph G=(V,E,W) be given,and let T be any spanning tree of G.

◦ Suppose that for every edge vw of G that is not in T,

◦ if uv is added to T, then it creates a cycle ◦ such that uv is a maximum-weight edge on that

cycle.◦ The the tree T is said to have the minimum

spanning tree property.

Properties of Minimum Spanning Trees … Lemma:

◦ In a connected, weighted graph G = (V, E, W), ◦ if T1 and T2 are two spanning trees that have the

MST property, ◦ then they have the same total weight.

Theorem: ◦ In a connected,weighted graph G=(V,E,W) ◦ a tree T is a minimum spanning tree if and only if ◦ T has the MST property.

Correctness of Prim’s MST Algorithm Lemma:

◦ Let G = (V, E, W) be a connected, weighted graph with n = |V|;

◦ let Tk be the tree with k vertices constructed by Prim’s algorithm, for k = 1, …, n; and

◦ let Gk be the subgraph of G induced by the vertices of Tk (i.e., uv is an edge in Gk if it is an edge in G and both u and v are in Tk).

◦ Then Tk has the MST property in Gk. Theorem:

◦Prim's algorithm outputs a minimum spanning tree.

Problem: Single-Source Shortest Paths Problem:

◦ Finding a minimum-weight path between two specified vertices

◦ It turns out that, in the worst case, it is no easier to find a minimum-weight path between a specified pair of nodes s and t than

◦ it is to find minimum-weight path between s and every vertex reachable from s. (single-source shortest paths)

Shortest-Path Definition: shortest path

◦ Let P be a nonempty path ◦ in a weighted graph G=(V,E,W) ◦ consisting of k edges xv1,v1v2,....vk-1y (possibly

v1=y).◦ The weight of P, denoted as W(P) is ◦ the sum of the weights,W(xv1),W(v1v2),...W(vk-1y).◦ If x=y, the empty path is considered to be a path

from x to y. The weight of the empty path is zero.◦ If no path between x and y has weight less than

W(P),◦ then P is called a shortest path,or minimum-

weight path.

Properties of Shortest Paths Lemma: Shortest path property

◦ In a weighted graph G, ◦ suppose that a shortest path from x to z consist of ◦ path P from x to y followed by◦ path Q from y to z.◦ Then P is a shortest path from x to y, and◦ Q is a shortest path form y to z.

Dijkstra’s Shortest-Path Algorithm

Greedy Algorithm weights are nonnegative

The Algorithm in action, e.g.

Correctness of Dijkstra’s Shortest-Path Algorithm Theorem:

◦ Let G=(V,E,W) be a weighted graph with nonnegative weights.

◦ Let V' be a subset of V and ◦ let s be a member of V'.◦ Assume that d(s,y) is the shortest distance in G from s

to y, for each yV'.◦ If edge yz is chosen to minimize d(s,y)+W(yz) over all

edges with one vertex y in V' and one vertex z in V-V',◦ then the path consisting of a shortest path from s to y

followed by the edge yz is a shortest path from s to z. Theorem:

◦ Given a directed weighted graph G with a nonnegative weights and a source vertex s, Dijkstra's algorithm computes the shortest distance from s to each vertex of G that is reachable from s.

Recommended