64
Graphs 1 Graphs Chapter 6 ORD DFW SFO LAX 8 0 2 1743 1843 1233 3 3 7

Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Embed Size (px)

Citation preview

Page 1: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 1

Graphs Chapter 6

ORD

DFW

SFO

LAX

802

1743

1843

1233

337

Page 2: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 2

Graph• A graph is a pair (V, E), where

– V is a set of nodes, called vertices– E is a collection (can be duplicated) of pairs of vertices, called edges– Vertices and edges are data structures and store elements

• Example:– A vertex represents an airport and stores the three-letter airport code– An edge represents a flight route between two airports and stores the mileage

of the route

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Page 3: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 3

Edge Types• Directed edge

– ordered pair of vertices (u,v)– first vertex u is the source or origin– second vertex v is the destination– e.g., a flight

• Undirected edge– unordered pair of vertices (u,v)– e.g., a flight route

• Directed graph (digraph)– all the edges are directed– e.g., flight network

• Undirected graph– all the edges are undirected– e.g., route network

ORD PVDflightAA 1206

ORD PVD849miles

Page 4: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 4

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.netqwest.net

math.brown.edu

cslab1bcslab1aApplications

• Electronic circuits– Printed circuit board

– Integrated circuit

• Transportation networks– Highway network

– Flight network

• Computer networks– Local area network

– Internet

– Web

• Databases– Entity-relationship diagram

Page 5: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 5

Terminology• End vertices (or endpoints) of an

edge– U and V are the endpoints of a

• Edges incident on a vertex– a, d, and b are incident on V

• Adjacent vertices – share edge– U and V are adjacent

• Degree of a vertex– X has degree 5

• Parallel edges (go between same nodes)– h and i are parallel edges

• Self-loop (same nodes is origin/destination)– j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 6: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 6

P1

Terminology (cont.)• Simple graphs have no parallel (multiple

edges between same vertices) or self loop• Path

– sequence of alternating vertices and edges which match up

• Cycle - path with same first and last vertex

• Simple path– path such that all its vertices and edges

are distinct

• Examples– P1=(V,b,X,h,Z) is a simple path– P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path

that is not simple

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 7: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 7

Terminology (cont.)• Simple cycle

– cycle such that all its vertices and edges are distinct

• Connected graph/component• (Spanning – contains all vertices)

subgraph • Forest (acyclic), free (no root) trees

(connected forest), spanning tree

• Examples– C1=(V,b,X,g,Y,f,W,c,U,a,) is a

simple cycle

– C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple

C1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hC2

Page 8: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 8

PropertiesNotation

n number of vertices

m number of edges

deg(v) degree of vertex v

Property 1

v deg(v) 2m

Proof: each edge is counted twice

Property 2In an undirected graph with

no self-loops and no multiple edges

m n (n 1)2Proof: each vertex has degree

at most (n 1)

What is the bound for a directed graph?

Example n 4 m 6 deg(v)

3

Page 9: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 9

Main Methods of the Graph ADT

• Vertices and edges– are positions

– store elements

• Accessor methods– aVertex()

– incidentEdges(v)

– endVertices(e)

– isDirected(e)

– origin(e)

– destination(e)

– opposite(v, e)

– areAdjacent(v, w)

• Update methods– insertVertex(o)

– insertEdge(v, w, o)

– insertDirectedEdge(v, w, o)

– removeVertex(v)

– removeEdge(e)

• Generic methods– numVertices()

– numEdges()

– vertices()

– edges()

Page 10: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Edge List Structure• We could just store a list of nodes and a list of edges.

• Vertex info: name, id, degree, …

• Edge info: directed?, endpoints, weight

• It wouldn’t be all that convenient for doing a traversal, but if our operations involved looking at each edge or each node, it would be simple.

• Note: edges point to vertexes (not visa versa)

10

Page 11: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 11

Edge List Structure

• Vertex object– element– reference to position in

vertex sequence

• Edge object– element– origin vertex object– destination vertex object– reference to position in

edge sequence

• Vertex sequence– sequence of vertex objects

• Edge sequence– sequence of edge objects

v

u

w

a c

b

a

zd

u v w z

b c d

Page 12: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Adjacency List Structure

• Edge list structure• Incidence sequence for each

vertex– sequence of references to edge

objects of incident edges

• Augmented edge objects– references to end vertices

12

Page 13: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 13

Adjacency Matrix Structure

• Edge list structure• Augmented vertex objects

– Integer key (index) associated with vertex

• 2D adjacency array– Reference to edge object

for adjacent vertices– Null for non nonadjacent

vertices

• The “old fashioned” version just has 0 for no edge and 1 for edge

u

v

w

a b

0 1 2

0

1

2 a

u v w0 1 2

b

Page 14: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Graphs 14

Asymptotic Performance

n vertices, m edges no parallel edges no self-loops Bounds are “big-

Oh”

Edge

List

AdjacencyList

Adjacency Matrix

Space n m n m n2

incidentEdges(v) m deg(v) n

areAdjacent (v, w)

m min(deg(v), deg(w)) 1

insertVertex(o) 1 1 n2

insertEdge(v, w, o)

1 1 1

removeVertex(v)

m deg(v) n2

removeEdge(e) 1 1 1

Page 15: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

15

Traversals

• Visit each node – e.g., web crawler• Have to restart traversal in each connected

component, but this allows us to identify components

• Reachability in a digraph is an important issue – the transitive closure graph

• Book permits counter-direction motion in general traversals

Page 16: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

What is meant by depth first search?

• Go deeper rather than broader

• Requires recursion or stack

• Used as a general means of traversal

16

Page 17: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Code for adjacency list implementation

class Node {

int ID;

String nodeLabel;

EdgeList adj; // successors

public String toString()

{ return ID + nodeLabel ;

}

}

17

class Edge { int from; // endpoint of start node int to; // endpoint of end node}

class EdgeList{ EdgeList next; Edge e;}

public class Graph{  final static int SIZE = 20; Node [] nodes = new Node[SIZE];}

Page 18: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

At your seats

Write the code to visit every node and print its name in the order it is visited. Note that with graphs, you may find yourself going in circles if you don’t mark the nodes as “visited” in some way.

18

Page 19: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

19

Looking for a spanning Tree

• Spanning tree: all of nodes and some of edges.

• Like a calling tree – make sure all are informed

• Many times algorithms depend on some ordering of the nodes of a graph

• Labeling the edges is some way is helpful in organizing thinking about a graph

Page 20: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

20

Depth-First Search (undirected graph)

• Simple recursive backtracking algorithm calls recursive function at starting point– For each incident edge to a vertex

• If opposite (other) vertex is unvisited – Label edge as “discovery”

– Recur on the opposite vertex

• Else label edge as “back”

• Discovery edges form a component spanning tree, back edges go to an ancestor

• with m edges, O(m) using an adjacency list, but not using an adjacency matrix

Page 21: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth First Search

• Notice in the next diagram that multiple steps are shown between each set of pictures.

• Discovery edges (tree edges) are drawn with solid black lines. Back edges are drawn with dashed lines.

• The current node is shown in solid black

21

Page 22: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

22

Depth-First Traversal

Page 23: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Given this code, find dfs numbers

class Node {

int ID;

int dfsNum;

String nodeLabel;

EdgeList adj; // successors

public String toString()

{ return ID + nodeLabel ;

}

}

23

class Edge { int from; // endpoint of start node int to; // endpoint of end node int edgeType: {BACK, TREE, CROSS, FORWARD}}

class EdgeList{ EdgeList next; Edge e;}

public class Graph{  final static int SIZE = 20; Node [] nodes = new Node[SIZE];}

Page 24: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 24

DFS Algorithm• The algorithm uses a mechanism for

setting and getting “labels” of vertices and edges

Algorithm DFS(G, v)Input graph G and a start vertex v of G Output labeling of the edges of G

in the connected component of v as discovery edges and back edges

setLabel(v, VISITED)for all e G.incidentEdges(v)

if getLabel(e) UNEXPLORED

w G.opposite(v,e)if getLabel(w)

UNEXPLOREDsetLabel(e, DISCOVERY)DFS(G, w)

elsesetLabel(e, BACK)

Algorithm DFS(G)Input graph GOutput labeling of the edges of G

as discovery edges andback edges

for all u G.vertices()setLabel(u, UNEXPLORED)

for all e G.edges()setLabel(e, UNEXPLORED)

for all v G.vertices()if getLabel(v)

UNEXPLOREDDFS(G, v)

Page 25: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 25

Example

DB

A

C

E

DB

A

C

E

DB

A

C

E

discovery edge

back edge

A visited vertex

A unexplored vertex

unexplored edge

Page 26: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 26

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

Page 27: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 27

DFS and Maze Traversal • The DFS algorithm is

similar to a classic strategy for exploring a maze– We mark each

intersection, corner and dead end (vertex) visited

– We mark each corridor (edge ) traversed

– We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

Page 28: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 28

Properties of DFSProperty 1

DFS(G, v) visits all the vertices and edges in the connected component of v

Property 2The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v

DB

A

C

E

Page 29: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Depth-First Search 29

Complexity of DFS

• DFS runs in O(n m) time provided the graph is represented by the adjacency list structure

Page 30: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

30

Biconnectivity (This material replaces that in section 6.3.2)

SEA PVD

MIASNA

ORDFCO

We worry about points of failure in the graph.

Page 31: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

31

Separation Edges and Vertices• Definitions

– Let G be a connected graph– A separation edge of G is an edge whose removal disconnects G – A separation vertex of G is a vertex whose removal disconnects G – A graph is bi-connected if for any two vertices of the graph there are two

disjoint paths between them (or a simple path with joins them)• Applications

– Separation edges and vertices represent single points of failure in a network and are critical to the operation of the network

• Example– DFW, LGA and LAX are separation vertices– (DFW,LAX) is a separation edge

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

Page 32: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Finding Articulation points1. Do a depth first search, numbering the nodes as they are visited in

preorder. Call the numbers num(v). If a node has already been visited, we consider the edge to it as a “back edge”. The undirected edges become “directed” by the order they are visited.

 2. The root is an articulation point if it has two children. Its removal would create two subtrees.

 

3. For each node, compute the lowest vertex that can be reached by zero or more tree edges followed by possibly one back edge.

Low(v) is the minimum of1. Num(v) [taking no edges]

2. the lowest back edge among all back edges (v,w) [no tree edges]

3. The lowest Low(w) among all tree edges (v,w) [some tree edges and a back edge]

32

Page 33: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

To compute Low, we need a postorder traversal.

Given Low, we find articulation points as:

1. The root is an articulation point if it has more than one child

2. Any other vertex v is an articulation point if and only if v has some child (w) in the tree such that Low(w) >=Num(v)

33

Page 34: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

In the example below, notice

• C is an articulation point as C has a child G and Low(G) >=Num(C).

• D is an articulation point as Low(E) >=Num(D).

34

Page 35: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

So how do we compute Low?

35

Page 36: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

What is the complexity?

• Assign DFS Numbers

• Assign Low

• Examine nodes for articulation point

If we don’t know which is greater n (the number of nodes) or m (the number of edges), we show it as O(n+m)

36

Page 37: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

37

Breadth-First Search• By levels, typically using queues

Page 38: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

38

BFS Facts• There are discovery (tree) and cross edges

(why no back edges?) –

Once marked, don’t follow again.

• Tree edges form spanning tree

• Tree edges are paths, minimal in length

• Cross edges differ by at most one in level

• Try writing the code to do a BFS

Page 39: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

39

Thm 6.19: Algorithms based on BFS

• Test for connectivity• compute spanning forest• compute connected components• find shortest path between two points (in number

of links)• compute a cycle in graph, or report none• (have cross edges)• Good for shortest path information, while DFS

better for complex connectivity questions

Page 40: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

40

Digraphs• A digraph is a graph

whose edges are all directed– Short for “directed graph”

• Applications– one-way streets

– flights

– task scheduling

Fundamental issue is reachability

A

C

E

B

D

Page 41: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Complexity

• For each node in a digraph, how would you see which other nodes are reachable from that node?

• What would the complexity be?

41

Page 42: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

42

Digraph Properties

• A graph G=(V,E) such that– Each edge goes in one direction:

• Edge (a,b) goes from a to b, but not b to a.

• If G is simple, m < n*(n-1) – at most an edge between each node and every other node.

• If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size.

A

C

E

B

D

Page 43: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

43

Digraph Application• Scheduling: edge (a,b) means task a must be

completed before b can be started

The good life

ics141ics131 ics121

ics53 ics52ics51

ics23ics22ics21

ics161

ics151

ics171

Page 44: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

44

Digraph Facts

• Directed DFS gives directed paths from root to each reachable vertex

• Used for O(n(n+m)) algorithm [dfs is O(n+m), these algorithms use n dfs searches]– Find all induced subgraphs (from each vertex, v, find

subgraph reachable from v)

– Test for strong connectivity

– Compute the transitive closure

• Directed BFS has discovery, back, cross edges

Page 45: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

45

Directed DFS• We can specialize the traversal

algorithms (DFS and BFS) to digraphs by traversing edges only along their direction

• In the directed DFS algorithm, we have four types of edges– discovery edges– back edges (to ancestor)– forward edges (to descendant)– cross edges (to other)

• A directed DFS starting at avertex s determines the vertices reachable from s A

C

E

B

D

Page 46: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

46

Reachability

• DFS tree rooted at v: vertices reachable from v via directed paths

A

C

E

B

D

FA

C

E D

A

C

E

B

D

F

Page 47: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

47

Strong Connectivity• Each vertex can reach all other vertices

a

d

c

b

e

f

g

Page 48: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

48

• Pick a vertex v in G.

• Perform a DFS from v in G.– If there’s a w not visited, return not

strongly connected

• Let G’ be G with edges reversed.

• Perform a DFS from v in G’.– If there’s a w not visited, return not

strongly connected

– Else, return strongly connected

• Running time: O(n+m).

Strong Connectivity Algorithm

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Page 49: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

49

• Maximal subgraphs such that each vertex can reach all other vertices in the subgraph

• Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity).

Strongly Connected Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 50: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

50

Transitive Closure• Given a digraph G, the

transitive closure of G is the digraph G* such that– G* has the same vertices as

G– if G has a directed path

from u to v (u v), G* has a directed edge from u to v

• The transitive closure provides reachability information about a digraph

B

A

D

C

E

B

A

D

C

E

G

G*

Page 51: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

51

Computing the Transitive Closure

• We can perform DFS starting at each vertex– O(n(n+m))

If there's a way to get from A to B and from B to C, then there's a way to get from A to C.

Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm

Page 52: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

52

Floyd-Warshall Transitive Closure

• Idea #1: Number the vertices 1, 2, …, n.• Idea #2: Consider paths that use only vertices

numbered 1, 2, …, k, as intermediate vertices:

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(add this edge if it’s not already in)

Page 53: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

53

Floyd-Warshall’s Algorithm• Floyd-Warshall’s algorithm

numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn

– G0=G

– Gk has directed edge (vi, vj) if G has directed path from vi to vj with intermediate vertices in the set {v1 , …, vk}

• We have that Gn = G*

• In phase k, digraph Gk is computed from Gk 1

• Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

Algorithm FloydWarshall(G)Input digraph G (vertices numbered)Output transitive closure G* of GG0 Gfor k 1 to n do

Gk Gk 1

for i 1 to n (i k) dofor j 1 to n (j i, k) do

if Gk 1.areAdjacent(vi vk) Gk 1.areAdjacent(vk vj)

if Gk.areAdjacent(vi vj) Gk.insertDirectedEdge(vi vj)

return Gn

Page 54: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

54

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 55: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

55

1 2 3 4 5 6 7

1 y

2

3 y y y

4 y

5 y y

6 y y

7 y y

Page 56: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

56

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 57: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

Recursion

• What if you didn’t want to use recursion to do a DFS?

• You could turn the parent edge around to tell you how to get back.

• This is termed doing a DFS “in-place”

• Obviously the edge would have to be flipped again after the call

57

Page 58: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

58

DAGs and Topological Ordering

• A directed acyclic graph (DAG) is a digraph that has no directed cycles

• A topological ordering of a digraph is a numbering

v1 , …, vn

of the vertices such that for every edge (vi , vj), we have i j

• Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints

Theorem

A digraph admits a topological ordering if and only if it is a DAG

B

A

D

C

E

DAG G

B

A

D

C

E

Topological ordering of

G

v1

v2

v3

v4 v5

Page 59: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

59

write c.s. program

play

Topological Sorting

• Number vertices, so that (u,v) in E implies u < v

wake up

eat

nap

study computer sci.

more c.s.

work out

sleep

dream about graphs

A typical student day1

2 3

4 5

6

7

8

9

1011

make cookies for professors

Page 60: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

60

For each vertex compute its indegree

Keep a set S of all vertices with indegree 0

num = 0;

While S is not empty

{ u = s.pop()

u.number = ++num;

for each of u’s successors w

w.indegree—

if (w.indegree ==0) S.add(w)

}

if (num <nodeCt) graph has a directed cycle (text differs)

Algorithm for Topological Sorting

Page 61: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

61

•Topological Sorting DFS Algorithm

• Simulate the algorithm by using depth-first search

• O(n+m) time.

Algorithm topologicalDFS(G, v)Input graph G and a start vertex v of G

(having no input arcs from unvisited nodes)Output labeling of the vertices of G

in the connected component of v setLabel(v,VISITED)for all edges (v,w)

if getLabel(w) !VISITEDtopologicalDFS(G, w)

Label v with topological number n n n - 1

Algorithm topologicalDFS(G)Input dag GOutput topological ordering of G

n G.numVertices()for all u G.vertices()

setLabel(u, !VISITED)for all v G.vertices() which have no incoming edges.

if getLabel(v) VISITED topologicalDFS(G, v)

Page 62: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

62

Topological Sorting Example

a

f

c

g

ei

d

b

h

Page 63: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

63

Topological Sorting Example

2

7

4

8

56

d

3

9

Page 64: Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337

64

Topological Sorting Example

2

7

4

8

56

1

3

9