graph algorithms terminology topological sort shortest-path algorithms minimum spanning tree graph...

74
Graph Algorithms • Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Upload: hilary-allen

Post on 27-Dec-2015

248 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Algorithms

• Terminology• Topological sort• Shortest-path algorithms• Minimum spanning tree• Graph traversals

Page 2: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

What is a Graph?• A graph G = (V, E) consists of:

– V: set of vertices– E: set of edges connecting the vertices in V

• An edge e = (v, w) is a pair of vertices, where v, w V. Sometimes each edge is associated with a weight or a cost.

• If the pair is ordered, then the graph is directed. Directed graphs are sometimes referred to as digraphs.

• Examples

a b

d e

c

V = {a, b, c, d, e}E = {(a, b), (a, c), (a, d), (b, e), (c, d), (c, e), (d, e)}

a b

d e

c

V = {a, b, c, d, e}E = {(a, b), (a, c), (a, d), (b, e), (c, d), (c, e), (d, e)} (pairs are ordered)

Page 3: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Road Network of McAllenUTPA (v1)

Expressway 281 (v13) McColl Depot (v7)

Expressway 83 (v11)

La Plaza Mall (v2)

Airport (v3)

Reynosa Bridge (v10)

S. 10th St. Depot 1 (v6)

N. 10th St. Depot 1 (v4)

23rd St. Depot (v12)

S. 10th St. Depot 2 (v5)

Nolana Depot 2 (v9)

Nolana Depot 1 (v8)

1.7 2.3

7.7

0.8

5.3

0.5

1.1

3.2

7.3

3.2

3.4

3.7

2.2

3.1

3.5

3.0

2.4

Page 4: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• Adjacent vertices: connected by an edge– Vertex w is adjacent to v if and only if (v, w) E. – In an undirected graph with edge (v, w), and hence (w, v), w is adjacent

to v and v is adjacent to w.

a b

d e

c

a b

d e

c

Vertex a is adjacent to c andvertex c is adjacent to a

Vertex c is adjacent to a, butvertex a is NOT adjacent to c

Page 5: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• Path– A path in a graph is a sequence of vertices w1, w2, w3, …, wN such that

wi+1 is adjacent to wi (i.e, (wi, wi+1) E) for 1 <= i < N.

– The length of a path, w1, w2, w3, …, wN, is the number of edges on the path, which is equal to N-1.

– We allow a path from a vertex to itself; if this path contains no edges, then the path length is 0.

a b

d e

c

a b

d e

c

abedce is a path.cdeb is a path.bca is NOT a path.

acde is a path.abec is NOT a path.

Page 6: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• Loops– If the graph contains an edge (v, v) from a vertex to itself, then the

path v, v is sometimes referred to as a loop.

– The graphs we will consider will generally be loopless.

• Simple paths– A simple path is a path such that all vertices are distinct, except that

the first and last could be the same.

a b

d e

c

abedc is a simple path.cdec is a simple path.abedce is NOT a simple path.

a b

d e

c

Page 7: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• Cycles– A cycle in a directed graph is a path of length at least 1 such that the

first vertex on the path is the same as the last one; if the path is simple, then the cycle is a simple cycle.

– A cycle in a undirected graph• A path of length at least 1 such that the first vertex on the path is the

same as the last one.• The edges on the path are distinct.

a b

d e

c

abeda is a simple cycle.abeceda is a cycle, but is NOT a simple cycle.abedc is NOT a cycle.

a b

d e

c

aba is NOT a cycle.abedceda is NOT a cycle.abedca is a cycle.

Page 8: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• DAGs (Directed acyclic graphs)– A directed graph is acyclic if it has no cycles.

a b

d e

c

a b

d e

c

Not a DAG A DAG

Page 9: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology

• Connectivity– An undirected graph is connected if there is a path from every vertex

to every other vertex.

– A directed graph is strongly connected if there is a path from every vertex to every other vertex.

a b

d e

c

a b

d e

c

a b

d e

c

a b

d e

Page 10: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Graph Terminology• Connectivity (cont)

– If a directed graph is not strongly connected, but the underlying graph (without direction to the arcs) is connected, then the graph is said to be weakly connected.

– A complete graph is a graph in which there is a edge between every pair of vertices.

a b

d e

c

a b

d e

c

a b

d c

a b

d c

How many edges are there in a complete graph with N vertices?

Undirected graph: N(N-1)/2 Directed graph: N(N-1)

Page 11: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Eulerian Tour Classical Graph Problem

“Can I walk across each bridge exactly once and return to the starting point?”

Eulerian tour: path that traverses every edge exactly once and return to the first vertex. Euler’s theorem: A graph has a Eulerian tour if and only if all vertices have even degree. Do you find such ideas interesting?

Page 12: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Representation of Graphs• Assumption

– Consider directed graphs (undirected graphs are similarly represented)

– All vertices are numbered starting at 1.

• Adjacency matrix representation• Adjacency list representation

1

3

6

4

2

7

5

Page 13: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Adjacency Matrix• A graph can be represented by using a two-dimensional

array A, called an adjacency matrix.• For each edge (u, v), we set A[u][v] to 1 (true);

otherwise the entry in the array is 0 (false).

• If the edge has a weight associated with it, then we can set A[u][v] equal to the weight and use either a very large or a very small weight as a sentinel to indicate nonexistent edges.

1

3

6

4

2

7

5

1 2 3 4 5 6 7

1 0 1 1 1 0 0 0

2 0 0 0 1 1 0 0

3 0 0 0 0 0 1 0

4 0 0 1 0 0 1 1

5 0 0 0 1 0 0 1

6 0 0 0 0 0 0 0

7 0 0 0 0 0 1 0

1

3

6

4

2

7

5

1

4

58

1

6 3

61

2

99

1 2 3 4 5 6 7

1 4 1 1

2 8 5

3 9

4 6 9 1

5 3 6

6

7 2

Page 14: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Adjacency Matrix Discussions • The adjacency matrix representation has the merit

of extreme simplicity.• However, the space requirement is (|V|2), which

can be prohibitive if the graph does not have very many edges.

• Dense graphs: a graph is dense if |E| = (|V|2).• Sparse graphs: if the graph is not dense, then it is

sparse.• An adjacency matrix is an appropriate

representation for a dense graph.• But, in most of the applications that we shall see,

the graph is sparse.

1

3

6

4

2

7

5

1 2 3 4 5 6 7

1 0 1 1 1 0 0 0

2 0 0 0 1 1 0 0

3 0 0 0 0 0 1 0

4 0 0 1 0 0 1 1

5 0 0 0 1 0 0 1

6 0 0 0 0 0 0 0

7 0 0 0 0 0 1 0|E| 4|V|3,000 intersections3,000 vertices12,000 edgesMatrix-size: 9,000,000

Page 15: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Adjacency Lists • An adjacency list is a better solution for a sparse graph.• For each vertex, we keep a list of all adjacent vertices.

• The space requirement is O(|E| + |V|), which is linear in the size of the graph.

• How to find all vertices adjacent to some given vertex v?

1

3

6

4

2

7

5

Page 16: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Adjacency Lists for Undirected Graphs• Each edge (u, v) appears in two lists; one is associated with u, the other is

associated with v. The space usage essentially doubles.

1

3

6

4

2

7

5

3 4 2

1 4 5

2 4 7

1 2 5 7 6 3

1 4 6

3 4 7

6 4 5

Page 17: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Topological Sort • What is a topological sort?

A topological sort is an ordering of vertices in a directed acyclic graph G, such that if there is a path from vi to vj in G, then vj appears after vi in the ordering.

• Example course prerequisite structure

– A directed edge (v, w) indicated that course v must be completed before taking course w.

– A topological ordering of these courses is any course sequence that does not violate the prerequisite requirement.

MAC3311

COP3210

CAP3700

MAD3104

COP3400

COP3212

COP455

MAD3305

MAD3512

COP3530

CDA4101

CDA4400

COP4540

COP5621

CIS4610

COP4610

COP4225

Page 18: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

More on Topological Sort • A directed graph with cycles has no topological orderings.

– If the graph has a cycle, then for two vertices v and w on the cycle, v precedes w and w precedes v.

• The topological ordering is not necessarily unique.

MAC3311

COP3210

CAP3700

MAD3104

COP3400

COP3212

COP455

MAD3305

MAD3512

COP3530

CDA4101

CDA4400

COP4540

COP5621

CIS4610

COP4610

COP4225

a b

d e

c

v1

v3

v6

v4

v2

v7

v5

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

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

Page 19: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

How to Find a Topological Ordering • The basic idea

– Find any vertex with no incoming edges.– Print this vertex, and remove it, along with its edges, from the graph.– Apply this same strategy to the rest of the graph, until no vertices

with 0 incoming edge.

• Example

v3

v6

v4

v2

v7

v5

v1v1

Output: v1

Output: v1 v2v2

Output: v1 v2 v5

v5Output: v1 v2 v5 v4v4

Output: v1 v2 v5 v4 v7

v7 Output: v1 v2 v5 v4 v7

v3

v3

Output: v1 v2 v5 v4 v7 v3

v6

v6

Page 20: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Cycle Detection • Using topological sorting to detect if there is a cycle in a

directed graph– Perform a topological sorting on the graph.– After finishing topological sorting, if there are remaining vertices in

the graph (i.e., each of those vertices has >= 1 incoming edge), then we can say the graph contains cycles.

• Example

v3

v6

v4

v2

v7

v5

v1v1

Output: v1

Output: v1 v2

v2

Output: v1 v2 v5v5 Output: v1 v2 v5 v4

v4

Output: v1 v2 v5 v4 v7v7v6

Page 21: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Topological Sorting Algorithm 1 • We define the indegree of a vertex v as the number of

edges (u, v).indegree(v1) = 0indegree(v4) = 3indegree(v7) = 2

• Assumptions– The indegree for each vertex is stored– The graph is read into an adjacency list

• The algorithmVoid Graph::topsort(){ Vertex v, w;

for (int counter = 0; counter < NUM_VERTICES; counter ++) {

v = findNewVertexOfDegreeZero(); if (v == NOT_A_VERTEX)

throw CycleFound(); v.topNum = counter; //place the topological numbering of the vertex. for each w adjacent to v w.indegree --;}

}

v3

v6

v4

v2

v7

v5

v1

0

1

23

1

3

2

Page 22: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Analysis of Algorithm 1 • The function findNewVertexOfIndegreeZero

scans the array of vertices looking for a vertex with indegree 0 that has not already been assigned a topological number.

• Since it is a simple sequential scan of the array of vertices, each call to it takes O(|V|) time. Totally there are |V| such calls, the running time of the algorithm is O(|V|2).

• Can we do better?– The cause of the poor running time is the sequential

scan through the array of vertices to find the vertex with indegree 0.

– In each iteration, we only update the indegrees of the vertices adjacent to the vertex with indegree 0. If the graph is sparse, we would expect that only a few vertices have their indegrees updated.

– However, in Algorithm 1, in the search for a vertex of indegree 0, we look at all the vertices.

– Actually, we only need to pay attention to the vertices with their indegrees updated in the last iteration.

v3

v6

v4

v2

v7

v5

v1

0

1

2

3

1

3

2

Page 23: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

A Better Implementation Algorithm 2 • The basic idea

– Use a queue to keep all the (unassigned) vertices of indegree 0

– Initially, we scan the array of vertices once to place all vertices of indegree 0 on the queue.

– In each iteration, remove the front from the queue and assign the topological numbering to the vertex.

– When we decrement the indegrees of the adjacent vertices, we check each vertex and place it in the rear of the queue if its indegree falls to 0.

• Note that the topological ordering then is the order in which the vertices dequeue.

v3

v6

v4

v2

v7

v5

v1

0

1

2

3

1

3

2

Page 24: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Pseudocode for Algorithm 2 void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w;

q.makeEmpty(); for each vertex v

if (v.indegree == 0) q.enqueue(v);

while (!q.isEmpty()) {

v = q.dequeue();v.topNum = ++counter; // assign next number as its topological numberingfor each w adjacent to v if (--w.indegree == 0) q.enqueue(w);

} if (counter != NUM_VERTICES)

throw CycleFound();}

Page 25: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Algorithm 2 Example

v3

v6

v4

v2

v7

v5

v1 0

1

2

3

1

3

2

1q:

top_ord:

1

0

1

2

1

3

22q:

top_ord: 1

Page 26: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Algorithm 2 Example

5q:

top_ord: 1 2

4q:

top_ord: 1 2 5

1

2

1

1

0

3

2

1

2

1

0

3

3

1

Page 27: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Algorithm 2 Example

3 7q:

top_ord: 1 2 5 4

1

2

0

4

3

2

0

7q:

top_ord: 1 2 5 4 3

1

2

5

4

3

1

0

Page 28: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Algorithm 2 Example

6q:

top_ord: 1 2 5 4 3 7

1

2

5

4

3

0

6

q:

top_ord: 1 2 5 4 3 7 6

1

2

5

4

3

7

6

Page 29: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Running Time of Algorithm 2 • The initialization steps take time O(|V|).

• The dequeue operations are done at most once per vertex. Totally, it takes O(|V|) time.

• The for loop is executed at most once per edge. It takes O(|E|) time.

• The total running time is O(|V|+|E|).

q.makeEmpty();

for each vertex v

if (v.indegree == 0)

q.enqueue(v);

v = q.dequeue();

v.topNum = ++counter; // assign next number as its topological numbering

for each w adjacent to v

if (--w.indegree == 0)

q.enqueue(w);

Page 30: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Shortest-Path Algorithms • Concepts• Dijkstra’s algorithm• Acyclic graphs

Page 31: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Weighted Graphs • Weights on the edges of a graph represent distances, costs, etc.• An example of a weighted undirected graph:

Page 32: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Weighted Paths • Given a weighted graph G = (V, E) with each edge (vi, vj) having a cost ci,j, the

cost of a path v1v2…vm is . This is referred to as the weighted path length.

• Generally, when it is not specified whether we are referring to a weighted or an unweighted path, the path is weighted if the graph is.

1

1 1,

m

i iic

Page 33: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Shortest Paths • The problem: Given a graph G = (V, E) with non-negative edge weights, and a

distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

v1 v6: 6

BOS LAX

Currently there are no algorithmsin which finding the path froms to one vertex is any faster than finding the path from sto all other vertices.

Page 34: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Road Network of McAllenUTPA (v1)

Expressway 281 (v13) McColl Depot (v7)

Expressway 83 (v11)

La Plaza Mall (v2)

Airport (v3)

Reynosa Bridge (v10)

S. 10th St. Depot 1 (v6)

N. 10th St. Depot 1 (v4)

23rd St. Depot (v12)

S. 10th St. Depot 2 (v5)

Nolana Depot 2 (v9)

Nolana Depot 1 (v8)

1.7 2.3

7.7

0.8

5.3

0.5

1.1

3.2

7.3

3.2

3.4

3.7

2.2

3.1

3.5

3.0

2.4

1.2

Page 35: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Dijkstra’s Algorithm• The problem: Given a graph G = (V, E) with non-negative edge weights, and a

distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

• Each vertex is marked as either known orunknown.

• A tentative distance dv is kept for each vertexto denote the shortest path length from s to v using only known vertices as intermediates.

• Use pv to record the predecessor on the path from s to v.• The key idea of Dijkstra’s algorithm:

– Dijkstra’s algorithm proceeds in stages.– At each stage,

• Selects a vertex v, which has the smallest dv among all the unknown vertices.

• Declares that the shortest path from s to v is known.• Updates the path length for the adjacent vertices

– The algorithm terminates while all vertices are labeled known.

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 36: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Updating the Path Length

• v is the newly evaluated vertex that has the smallest dv among all the unknown vertices.

• For each adjacent vertex w, ifdw > dv + cv,w,

then we can improve the estimate of dw:

dw = dv + cv,w

• It is a good idea to use v on the path to w.

s

v

w

60

30 20

Page 37: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrating Dijkstra’s Algorithm• Find the shortest paths from start node v1(s).

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 F 0 0v 2 F 0v 3 F 0v 4 F 0v 5 F 0v 6 F 0v 7 F 0

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

v1 is selected with path length 0.

Page 38: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrating Dijkstra’s Algorithm

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 F 2 v1

v 3 F 0v 4 F 1 v1

v 5 F 0v 6 F 0v 7 F 0

v4 is selected.

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 F 2 v1

v 3 F 3 v4

v 4 T 1 v1

v 5 F 3 v4

v 6 F 9 v4

v 7 F 5 v4

v2 is selected.

Update v2, v4

Update v3, v5, v6, v7

Page 39: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrating Dijkstra’s Algorithm

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 F 3 v4

v 4 T 1 v1

v 5 F 3 v4

v 6 F 9 v4

v 7 F 5 v4

v3 is selected.

Update v5

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 T 3 v4

v 4 T 1 v1

v 5 F 3 v4

v 6 F 8 v3

v 7 F 5 v4

v5 is selected.

Update v6

Page 40: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrating Dijkstra’s Algorithm

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 T 3 v4

v 4 T 1 v1

v 5 T 3 v4

v 6 F 8 v3

v 7 F 5 v4

v7 is selected.

Update v7

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 T 3 v4

v 4 T 1 v1

v 5 T 3 v4

v 6 F 6 v7

v 7 T 5 v4

v6 is selected.

Update v6

Page 41: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrating Dijkstra’s Algorithm

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 T 3 v4

v 4 T 1 v1

v 5 T 3 v4

v 6 T 6 v7

v 7 T 5 v4

Update

2

1

2 2

4

1

v3

v6

v4

v2

v7

v5

v1

Page 42: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Dijkstra’s Algorithm Pseudocodevoid Graph::dijkstra(Vertex s){

Vertex v, w;/* 1*/ s.dist = 0;

/* 2*/ for ( ; ; ){

/* 3*/ v = smallest unknown distance vertex;/* 4*/ if ( v == NOT_A_VERTEX)/* 5*/ break;/* 6*/ v.known = true;

/* 7*/ for each w adjacent to v/* 8*/ if (!w.know)/* 9*/ if (v.dist + cvw < w.dist)

{/* 10*/ decrease (w.dist to v.dist + cvw);

/* 11*/ w.path = v;}

}}

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 43: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Running Time• The running time depends on how the vertices are

manipulated.• If we use the obvious algorithm of scanning down

the array of vertices to find the minimum dv, – Each phase will take O(|V|) time to find the minimum– Thus, O(|V|2) time will be spent finding the minimum

over the course of the algorithm.

• The time for updating dw is constant per update, and there is at most one update per edge for a total of O(|E|).

• The total running time is O(|E|+|V|2) = O(|V|2).• Good for dense graphs.

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

v1

v2

v3

v4

v5

v6

v7

Page 44: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Running Time• Using priority queues

use a priority queue to keep the dv’s for all unknown vertices.

• Perform a deleteMin operation to obtain the minimum dv, which takes O(log|V|) time.

• Updating dw is treated as a decreaseKey operation on a priority queue, which takes O(log|V|) time.

• How to find the corresponding dv for a vertex v?

• Total running time: O((|V|+|E|)log|V|).• Using the Fibonacci heap to implement

Dijkstra’s algorithm, the running time is O(|E|+|V|log|V|).

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1

v1

v2

v3

v4

v5

v6

v7

2 v2

3 v3 3 v5

9 v6 5 v7

Page 45: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Negative Edge Costs• Dijkstra’s algorithm is NOT applicable to a graph with negative edge costs.

• The Bellman-Ford algorithm solves the shortest path problem in a graph with negative edge costs in O(|V||E|) time.

v3

v2

v1

5

8-6

Page 46: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Printing the Shortest Paths

• Track back the start vertex s by using pv.

• Example: print out the shortest path from s (v1) to v6.

v6 v7 v4 v1 v1 v4 v7 v6

4

2

103

1

2 2

64

1

85

v3

v6

v4

v2

v7

v5

v1v known d v p vv 1 T 0 0v 2 T 2 v1

v 3 T 3 v4

v 4 T 1 v1

v 5 T 3 v4

v 6 T 6 v7

v 7 T 5 v4

Page 47: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Shortest Paths in Directed Acyclic Graphs• What is a directed acyclic graph?

A directed acyclic graph (DAG) is a directed graph with no cycles.• Shortest paths are always well defined in a DAG, even if there are negative-cost

edges (no negative-cost cycles can exist).• The algorithm

void Graph::shortestPathinDAG (Vertex s){

Vertex v, w;

/* 1*/ s.dist = 0;for any other vertex v, v.dist = ;

/* 2*/ topologically sort the vertices of G;/* 3*/ for each vertex v taken in topologically sorted order/* 4*/ do for each vertex w adjacent to v/* 5*/ do if (v.dist + cvw < w.dist)

{/* 6*/ decrease (w.dist to v.dist + cvw);/* 7*/ w.path = v;

}}

r s v u w x5 2 7 -1 -2

3

6 1

4

2

Page 48: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustrations

r s v u w x5 2 7 -1 -2

3

6 1

4

2

Find shortest paths from s to all other vertices.

Step 1: Initialization:

r s v u w x5 2 7 -1 -2

3

6

4

2

0

Step 2: Topologically sorting:

r s v u w x5 2 7 -1 -2

3

6

4

2

0

1

1

Page 49: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

IllustrationsStep 3: Performing one pass over the vertices in the topologically

sorted order and updating the distances:

r s v u w x5 2 7 -1 -2

3

6

4

2

0

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6 6 4

1

1

1

Page 50: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

IllustrationsStep 3: Performing one pass over the vertices in the topologically

sorted order and updating the distances:

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6 5 4

1

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6 5 3

1

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6 5 3

1

Page 51: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Analysis• Correctness

When a vertex v is selected, its distance v.dist can no longer be lowered, since by the topological ordering rule it has no incoming edges emanating from unknown vertices.

• Running time– Initialization step takes O(|V|) time.– Topologically sorting takes O(|V|+|E|) time.– In step 3, one pass over the vertices takes O(|V|) time; It totally takes O(|E|)

to examine each edge once for distance updating.– Totally, the running time is O(|V|+|E|).

r s v u w x5 2 7 -1 -2

3

6

4

2

0 2 6 6 4

1

Page 52: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Minimum Spanning Tree• Motivation

– A town has a set of houses and a set of roads– A road connects 2 and only 2 houses– A road connecting houses u and v has a

repair cost c(u, v)– Objective: Repair enough (and no more) roads

such that• Everyone stays connected: can reach every house from all other houses,

and• Total repair cost is minimum.

• Model as a graph– Undirected graph G = (V, E)– Weight c(u, v) on each edge (u, v) E– Find T E such that

• T connected all vertices (T is a spanning tree), and• c(T) = (u, v)T c(u, v) is minimized.

4

2

10

31

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 53: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Minimum Spanning Tree

• Given an undirected graph G = (V, E), with edge costs cij.• A spanning tree T of G is an acyclic subgraph that spans all the

vertices.

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

1

64

1

8

v3

v6

v4

v2

v7

v5

v1

4

2

101

4v3 v4

v2

v7

v5

v1

4

2

7

45

v3

v6

v4

v2

v7

v5

v110

31

2

1

5

v3

v6

v4

v2

v7

v5

v1

Page 54: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Minimum Spanning Tree• What is the number of edges in a spanning tree with |V|

vertices? • The cost of the spanning tree T is the sum of the costs of the

edges in T.• The minimum spanning tree (MST) is the smallest cost

spanning tree

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v110

31

2

1

5

v3

v6

v4

v2

v7

v5

v1

4

2

101

68

v3

v6

v4

v2

v7

v5

v1

|V|-1

Page 55: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Prim’s Algorithm• Key idea

– Grows the minimum spanning tree in successive stages– At any point in the algorithm, we have a set of vertices that

have already been included in the tree; the rest of the vertices have not.

– The algorithm then finds, at each stage, a new vertex to add to the tree by choosing the edge (u, v) such that the cost of (u, v) is the smallest among all edges where u is in the tree and v in not.

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 56: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Simple Way to Represent a Tree• In a tree, each vertex has only one parent.• Each vertex keeps a pointer pointing to its parent.

• We use pv to denote the parent of v in the tree.

4

2

101

68

v3

v6

v4

v2

v7

v5

v1

Page 57: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Pseudocode of Prim’s Algorithm• Input: Graph G = (V, E), and s is the root of the minimum

spanning tree. For each vertex v, A(v) is a list of its adjacent vertices.

• Let dv denote the cost of the shortest edge connecting v to a vertex in the tree.

• Let Q denote the vertices not in the tree.• The algorithm

void Graph::MST-Prim (Vertex s){

Q = V;for each v Q do dv = ;ds = 0; ps = null; while Q do find a vertex v Q incident on a shortest edge connecting with a vertex in the tree; add v into the tree and remove v from Q; for each vertex w adjacent to v do if w Q and cvw < dw {

dw = cvw; pw = v; }

}

v1

v2

v3

v4

v5

v6

v7

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

71

48

2

Page 58: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration• Grow a minimum spanning tree of G with root v1.

• dv denote the cost of the shortest edge connecting v to a vertex in the tree.

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

1

4

Page 59: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

71

48

2

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

71

48

2

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

71

45

2

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

61

41

2

Page 60: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

61

41

2

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

0 2

61

41

2

Page 61: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Running Time of Prim’s Algorithm• Depend on how to implement Q.• The algorithm

void Graph::MST-Prim (Vertex s){

Q = V;for each v Q do dv = ;

ds = 0; ps = null;

while Q do find a vertex v Q incident on a shortest edge connecting with a vertex in the tree; add v into the tree and remove v from Q; for each vertex w adjacent to v do if w Q and cvw < dw {

dw = cvw;

pw = v;

}}

• Using an array or a list: O(|V|2)• Using a binary heap: O(|E|log|V|)

v1

v2

v3

v4

v5

v6

v7

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 62: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Kruskal’s Algorithm• Initially make each vertex of V a singleton

tree.• Scan edges of E in non-decreasing order of

cost.• For edge e:

– If both endpoints of e in the same tree, then discard it;

– Otherwise, add e and merge the two trees.

v1

v2

v3

v4

v5

v6

v7

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 63: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration• Grow a minimum spanning tree of G by using Kruskal’s algorithm.

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 64: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 65: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Pseudocode of Kruskal’s Algorithm• Input: Graph G = (V, E. For each vertex v, A(v) is a list of

its adjacent vertices.• The algorithm

void Graph::MST-Kruskal (Vertex s){

view each vertex in V as a singleton tree;sort the edges of E by nodecreasing cost c;for each edge (v, w) E, in order by nodecreasing cost do if v and w are not in the same tree then merge the two trees;

}

• Running Time: – Using disjoint set data structure, O(|E|log|V|).

v1

v2

v3

v4

v5

v6

v7

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

4

2

103

1

2 7

64

1

85

v3

v6

v4

v2

v7

v5

v1

Page 66: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Breadth First Search• The problem

– Input: Graph G = (V, E), either directed or undirected, and source vertex s V.

– Output: d[v] = distance (smallest # of edges) from s to v, for all v V.• Idea: Send a wave out from s.

– First hits all vertices 1 edge from s.– From there, hits all vertices 2 edges from s.– Etc.

s

c

a

b

e

f

g

h

q

0

1

3

1

2

3

2

3

3

Page 67: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Breadth First Search• Use FIFO queue Q to maintain wavefront.

– v Q if and only if wave has hit v but has not come out of v yet.BFS(G, s)for each u V-{s} do d[u] = ; [u] = nulld[s] = 0; [s] = nullQ = ENQUEUE(Q, s)while Q do u = DEQUEUE(Q) for each v Adj[u] do if d[v] =

then d[v] = d[u] + 1 [v] = u ENQUEUE(Q, v)

• Example:

s

c

a

b

e

f

g

h

q

0

1

3

1

2

3

2

3

3

Page 68: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Breadth First Search• Can show that Q consists of vertices with d values.

i i i … i i+1 i+1 … i+1 – Only 1 or 2 different values– If 2, differ by 1 and all smallest are first.

• Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time.

• Running Time: O(|V|+|E|)– O(|V|) because every vertex enqueued at most once.– O(|E|) because every vertex dequeued at most once and we examine (u, v)

only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

Page 69: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Depth-First Search• Depth-first search (DFS) on undirected graphs

– We start at vertex v, mark v as visited. – Recursively call depth-first search on all adjacent vertices that are not

already visited.

• Pseudocode for DFS on undirected graphsvoid Graph::dfs(vertex v){ v.visited = true; for each w adjacent to v

if (!w.visited) dfs(w);

}

• DFS on graphs is similar to preorder traversal on a tree.

Page 70: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration of DFS on Undirected Graphs• DFS graph G starting at vertex A

A

B D

C

E

Depth-first spanning tree

AA

BB

CC

D D EE

Page 71: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

AB

DC

E FG

Depth-First Spanning Tree with Back Edges• DFS graph G starting at vertex C

AA

BB

EE

FF

GG

CC

• The root of the tree is C, the first vertex visited.• Each edge (v, w) in the graph is present in the tree.

DD

• If, when we process (v, w), we find that w is unmarked, we indicate this with a tree edge.• If, when we process (v, w), we find that w is already marked, we draw a dashed line, which we call a back edge.

Page 72: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Running Time of DFS• DFS is called on each vertex exactly once.• For every edge is examined exactly twice, once from each of its

vertices.• Total running time: O(|V|+|E|)

Page 73: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

DFS on Directed Graphs

• Using the same strategy as with undirected graphs, directed graphs can be traversed in linear time, using depth-first search.

• If the graph is not strongly connected, a depth-first search starting at some node might not visit all nodes.

• In this case we repeatedly perform depth-first searches, staring at some unmarked node, until all vertices have been visited.

Page 74: Graph Algorithms Terminology Topological sort Shortest-path algorithms Minimum spanning tree Graph traversals

Illustration of DFS on Directed Graphs

• DFS on directed graph G starting at vertex B

BA

CD F

G

E

H

I J

B

B

G

G

D

D

C

C

E

E

H

H

A

A

F

F

I

I

J

J

Depth-first spanning forest