Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall...

131
Introduction To Graphs D. Thiebaut CSC212 — Fall 2014

Transcript of Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall...

Page 1: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Introduction To Graphs

D. Thiebaut CSC212 — Fall 2014

Page 2: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Formal Definition

• G = (V, E)

• V is the set of vertices

• E is the set of edges

Page 3: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Directed Graphs

• G = (V, E)

• Each element e of E is an ordered pair (vi, vj)

vi vj

e

Page 4: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Undirected Graphs

• G = (V, E)

• For each edge (vi, vj) of E , there is an edge (vj, vi)

vi vj

Page 5: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

How Do We Store Graphs?

• Adjacency List

• Adjacency Matrix

• Linked Nodes

• Incidence Matrix

Page 6: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Adjacency List

Page 7: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Adjacency Matrix

Page 8: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Incidence Matrix

Why the different data-structure?

Page 9: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Terminology

Page 10: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

1 2

34

Path 12

34

Disconnected

12

cycle

34

1 2connected

345

Page 11: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

vi

vk

e

e emanate from vi out-degree of vi is 4

e is incident to Vk in-degree of vk is 3

Page 12: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Java Implementation

Page 13: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Java Review: Iterators

Page 14: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

public static void main(String[] args) { ArrayList<Integer> array = new ArrayList<Integer>(); for ( int i=0; i<5; i++ ) array.add( i*2 + 1 );

Iterator<Integer> it = array.iterator();

while ( it.hasNext() ) {

int x = it.next();

System.out.print( x + “ “ ); } }

123

1 3 5 7 9

Page 15: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

public static void main(String[] args) { ArrayList<Integer> array = new ArrayList<Integer>(); for ( int i=0; i<5; i++ ) array.add( i*2 + 1 );

for ( int x: array ) { System.out.println( x );}

}

1

1 3 5 7 9

Page 16: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Java Implementation(one option)

Page 17: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

public class Graph1 {private boolean[][] adjMat;private int noVertices;private int noEdges;private Set<Integer> vertices;private boolean visited[]; private int count;

public Graph1( int V ) {adjMat = new boolean[V][V]; // allocated to false by defaultnoVertices = V;noEdges = 0;vertices = new TreeSet<Integer>();

}

public void addEdge( int v1, int v2 ) {vertices.add( v1 );vertices.add( v2 );if (!adjMat[ v1 ][ v2 ])

noEdges++;

adjMat[ v1 ][ v2 ] = true;adjMat[ v2 ][ v1 ] = true;

}

Page 18: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

public boolean contains( int v1, int v2 ) {return adjMat[ v1 ][ v2 ];

}

// return list of neighbors of v public Iterable<Integer> adj( int v ) { return new AdjMatIterator(v); }

Page 19: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

// support iteration over graph vertices private class AdjMatIterator implements Iterator<Integer>, Iterable<Integer> { int v, w = 0;

AdjMatIterator(int v) { this.v = v; }

public Iterator<Integer> iterator() { return this; } public boolean hasNext() { while ( w < noVertices ) { if ( adjMat[ v ][ w ] ) return true; w++; } return false; } public Integer next() { if ( hasNext() ) return w++; else return null; } }

Page 20: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

/** * (0) --- (1) --- (2) * | | \ * | | \ * (3) --- (4) (5) --- (6) * | | * (7) --- (8) * */

public static void main(String[] args) {Graph1 G = new Graph1(9);G.addEdge( 0, 1 ); G.addEdge( 1, 2 ); G.addEdge( 3, 4 );G.addEdge( 5, 6 ); G.addEdge( 7, 8 ); G.addEdge( 0, 3 );G.addEdge( 1, 4 ); G.addEdge( 1, 5 ); G.addEdge( 3, 7 );G.addEdge( 4, 8 );

System.out.print( "Vertices ajacent to 1: " );

for ( int v: G.adj( 1 ) ) System.out.print( v + " " );

}}

Vertices ajacent to 1: 0 2 4 5

Page 21: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DFS: Depth First

Search

Page 22: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

Page 23: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

Page 24: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

Page 25: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

Page 26: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

Page 27: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

Page 28: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

Page 29: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

Page 30: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

Page 31: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

Page 32: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

Page 33: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 34: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 35: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 36: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 37: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 38: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

Page 39: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

0 1 2 3 4 5 6

1,4,6

5,2,4

1

1,2,6,3

2,4,0

3, 6

0,4

DONE!

Page 40: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

What can we use DFS for?

What property of the graph can we test

with DFS?

Page 41: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Java Implementation

Page 42: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

private void recurseDFS( int v ) { visited[ v ] = true;

for ( int w: adj( v ) ) if ( ! visited[w] ) recurseDFS( w ); } public void DFS( int v ) { visited = new boolean[ noVertices ]; recurseDFS( v ); }

Skeleton

Page 43: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Complexity

Page 44: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

• Must initialize data structure for each vertex O( … )

• Must follow each edge emanating from each vertex O( … )

Complexity

Page 45: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Lab: Play with DFSLA

B TIME

Page 46: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

We stopped here last time…

Page 47: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

BFS Breadth

First Search

Page 48: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Example

1

2

34

5 6

0

Page 49: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

2

Page 50: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

2

Page 51: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

2

1, 4, 6

Page 52: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

1 4 6

1, 4, 6

Page 53: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

4 6

1, 4, 6

1

5, 2

Page 54: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

4 6 5

1, 4, 6

5, 2

Page 55: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

6 5

1, 4, 6

4

5, 2 2, 6

Page 56: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

5

1, 4, 6

6

5, 2 2, 6

4, 0

Page 57: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

5 0

1, 4, 6

5, 2 2, 6

4, 0

Page 58: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

0

1, 4, 6

5, 2 2, 6

4, 0

5

1

Page 59: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

1, 4, 6

5, 2 2, 6

4, 0

0

1

6, 3

Page 60: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

3

1, 4, 6

5, 2 2, 6

4, 01

6, 3

Page 61: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

1, 4, 6

5, 2 2, 6

4, 01

6, 3

3

0

Page 62: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

0 1 2 3 4 5 6

1

2

34

5 6

0

1, 4, 6

5, 2 2, 6

4, 01

6, 3

0

Page 63: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Observations• Both DFS and BFS create spanning trees with the

edges they visit.

• The trees are rooted at the first vertex they start with.

• DFS works best as a recursive function

• BFS works best with a queue

Page 64: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Weighted Graphs

Page 65: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Weighted Graphs: Definitions

• Each edge e has a weight

• The cost of a path through linked vertices is the sum of the weights of the edges on that path.

Page 66: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

1

2

34

5 6

0

5

5 1

3

2 2

1

9

2

Page 67: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Finding the Shortest Path from a Vertex to

the Other Vertices in its Component

Proble

m

of the D

ay

Page 68: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Dijkstra’s Algorithm

Page 69: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

The Basic Idea

330

315

Page 70: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

Page 71: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

1

2

34

5 6

0

5

5 1

3

2 2

1

9

Shortest Path from 5 to 3?

1

Shortest Path from 5 to all?

Page 72: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

1

2

34

5 6

0

5

51

3

2 2

1

9

Visited

Cost

0 1 2 3 4 5 6

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

∞ ∞ ∞ ∞ ∞ 0 ∞

1

Page 73: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ ∞ ∞ ∞ ∞ 0 ∞

1

Visited

Page 74: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 ∞ ∞ ∞ 0 ∞

1

Visited

Page 75: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 ∞ ∞ ∞ 0 ∞

1

Visited

Page 76: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 ∞ 8 0 ∞

1

Visited

Page 77: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 ∞ 8 0 ∞

1

Visited

Page 78: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 ∞ 8 0 11

1

Visited

Page 79: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 ∞ 8 0 11

1

Visited

Page 80: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 17 8 0 11

1

Visited10

Page 81: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

∞ 5 6 17 8 0 11

1

Visited10

Page 82: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

11 5 6 17 8 0 11

1

Visited10

Page 83: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

11 5 6 17 8 0 11

1

Visited10

Page 84: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

11 5 6 17 8 0 11

1

Visited10

12

Page 85: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost mark v as visited for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

1

2

34

5 6

0

5

51

3

2 2

1

9

Cost

0 1 2 3 4 5 6

11 5 6 17 8 0 11

1

Visited10

12

Page 86: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

1

2

34

5 6

0

Page 87: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

How do we implement the collection of all unvisited vertices?

How do we quickly find the vertex with the lowest cost?

Page 88: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Complexity of Dijkstra

Page 89: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

Page 90: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

?

Page 91: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

Page 92: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

?

O(V)

Page 93: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

Page 94: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

?

O(V)

Page 95: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

O(V)

Page 96: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

?

O(V)

Page 97: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

O(log(V))

O(V)

Page 98: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

?

O(V)

Page 99: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

O(V)

O(V)

Page 100: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

DijkstraAlgorithm( G, start ) for all vertices v in G cost( v ) = ∞;

cost( start ) = 0; unvisited = all vertices in G while ( unvisited not empty ) v = unvisited vertex with lowest cost unvisited = unvisited - v for all unvisited w adjacent to v if cost( w ) > cost(v) + weight(v,w) cost( w ) = cost(v) + weight(v,w) predecessor( w ) = v

O(V)

O(V)

O(V)O(E + V.log(V))

O(V)

Page 101: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

• Dijskra’s Shortest Path Algorithm: O( … )

Complexity

Page 102: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Dijskra will fail…

• If graph contains edges with negative weights

Page 103: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Dijkstra in Java

Page 104: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

class Vertex implements Comparable<Vertex> {public final int Id;public Edge[] adj;public double minDistance = Double.POSITIVE_INFINITY;public Vertex predecessor;

public Vertex( int i ) { Id = i; }public String toString() { return Id+" "; }public int compareTo(Vertex other) {

return Double.compare(minDistance, other.minDistance);}

}

Page 105: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

class Edge {public final Vertex source;public final Vertex dest;public final double weight;

public Edge(Vertex argSource, Vertex argTarget, double argWeight) {

source = argSource;dest = argTarget;weight = argWeight;

}}

Page 106: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

public class Graph2 {Vertex[] vertices;

Graph2() {vertices = null;

}

public static void dijkstraPaths(Vertex source) {source.minDistance = 0.;PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();vertexQueue.add(source);

while ( !vertexQueue.isEmpty() ) {Vertex u = vertexQueue.poll();

// Visit each edge emanating from ufor ( Edge e : u.adj ) {

Vertex v = e.dest;double weight = e.weight;double distanceThroughU = u.minDistance + weight;if (distanceThroughU < v.minDistance) {

vertexQueue.remove(v);v.minDistance = distanceThroughU;v.predecessor = u;vertexQueue.add(v);

}}

}}

…}

Page 107: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

All-Pair Shortest Paths Warshall, Floyd &

Ingerman’s Algorithm

Page 108: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Algorithm WFIAlgorithm( matrix weight) for i = 0 to V-1 // V= # vertices

for j = 0 to V-1 for k = 0 to V-1 if weight[j][k] > weight[j][i] + weight[i][k] weight[j][k] = weight[j][i] + weight[i][k]

j

i

k

Page 109: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

• Warshall, Floyd, Ingerman’s Algorithm: O( … )

Complexity

Page 110: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Topological Sort

Which course should I take

first?

Page 111: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Basic IdeatopologicalSort( digraph ) for v in V: find a vertex v with empty adjacency list number( v ) = i++

remove v and all edges incident to v from consideration

Page 112: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Algorithm (1)

topologicalSort( digraph ) { for all v in V: num(v) = 0; TSnum(v) = 0; i = 0; j = 0; while there is v s.t. num(v)==0 TS( v ); return list of v sorted by TSnum;}

Page 113: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Algorithm (2)

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

Page 114: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

Algorithm (2)

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

?

Page 115: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

0

0

b

0

0

c

0

0

d

0

0

e

0

0

f

0

0

g

0

0

num

TSnum

Page 116: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

0

0

c

0

0

d

0

0

e

0

0

f

0

0

g

0

0

num

TSnum

Page 117: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

0

0

c

2

0

d

0

0

e

0

0

f

0

0

g

0

0

num

TSnum

Page 118: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

0

0

c

2

0

d

3

0

e

0

0

f

0

0

g

0

0

num

TSnum

Page 119: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

0

c

2

0

d

3

0

e

0

0

f

0

0

g

0

0

num

TSnum

Page 120: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

0

c

2

0

d

3

0

e

5

0

f

0

0

g

0

0

num

TSnum

Page 121: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

0

c

2

0

d

3

0

e

5

0

f

0

0

g

6

0

num

TSnum

Page 122: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

0

c

2

0

d

3

0

e

5

0

f

0

0

g

6

1

num

TSnum

Page 123: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

0

c

2

0

d

3

0

e

5

2

f

0

0

g

6

1

num

TSnum

Page 124: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

0

d

3

0

e

5

2

f

0

0

g

6

1

num

TSnum

Page 125: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

0

d

3

0

e

5

2

f

0

0

g

6

1

num

TSnum

Page 126: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

0

d

3

0

e

5

2

f

7

0

g

6

1

num

TSnum

Page 127: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

0

d

3

0

e

5

2

f

7

4

g

6

1

num

TSnum

Page 128: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

0

d

3

5

e

5

2

f

7

4

g

6

1

num

TSnum

Page 129: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

0

b

4

3

c

2

6

d

3

5

e

5

2

f

7

4

g

6

1

num

TSnum

Page 130: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

a

1

7

b

4

3

c

2

6

d

3

5

e

5

2

f

7

4

g

6

1

num

TSnum

Page 131: Introduction To Graphs - Clark Science Center · Introduction To Graphs D. Thiebaut CSC212 — Fall 2014. Formal Definition

TS( v ) { num(v) = i++; for all u adjacent to v if num(u) == 0 TS( u ); else if TSNum(u) == 0 error(“Cycle detected!”) TSNum(v) = j++;}

a

c d

b

e

gf

TSnum: a b c d e f g 7 3 6 5 2 4 1

a c d f b e g g e b f d c a