graphs

90
Graphs

Upload: aminia

Post on 21-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Graphs. Graphs are not function plots. Example:. a. b. V = {a,b,c,d,e} E = {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}. c. e. d. What is a Graph?. A graph G is a pair ( V , E ) where V : set of vertices E : set of edges connecting the vertices in V - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graphs

Graphs

Page 2: Graphs

Graphs are not function plots.

1stQtr

2ndQtr

3rdQtr

4thQtr

East

North0

20

40

60

80

100

East

West

North

Page 3: Graphs

What is a Graph?• A graph G is a pair (V,E)

where V: set of vertices

E: set of edges connecting the vertices in V

Vertices and edges are positions and store elements• An edge e = (u,v) is a pair of vertices

a b

c

d e

V= {a,b,c,d,e}

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

Example:

•Graph is a more general data structure. Tree is a special case of graph

Page 4: Graphs

Graph: 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

MCODFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

9501120

1233337

2555

142

Page 5: Graphs

John

DavidPaul

ucf.edu

ATI.com

cs.ucf.edu

att.netsprint.net

math.ucf.edu

VisionGraphicsGraph: Examples

• Computer networks– Local area network– Internet– Web

• Electronic circuits– Printed circuit board– Integrated circuit

• Transportation networks– Highway network– Flight network

• Databases– Entity-relationship diagram

Page 6: Graphs

Edge Types• Directed edge

– ordered pair of vertices (u,v)– first vertex u is the 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– all the edges are directed– e.g., flight network

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

MCO LGAflight

AA 1348

MCO LGA950

miles

Page 7: Graphs

7

Graph Terminology• adjacent vertices: vertices

connected by an edge• degree (of a vertex): # of

adjacent vertices

NOTE: The sum of the degrees of all vertices is twice the number of edges. Why?

Since adjacent vertices each count the adjoining edge, it will be counted twice

• path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent.

a

d e

c

b

a b

c

d e

a b

c

d e

a b e d c b e d c

3

23

3

3

Page 8: Graphs

More Graph Terminology• simple path: no repeated vertices

• cycle: simple path, except that the last vertex is the same as the first vertex

a b

c

d e

b e c

a c d a

a b

c

d e

a c d a

Page 9: Graphs

Subgraphs• A subgraph S of a

graph G is a graph such that – The vertices of S are a

subset of the vertices of G

– The edges of S are a subset of the edges of G

• A spanning subgraph of G is a subgraph that contains all the vertices of G

Subgraph

Spanning subgraph

Page 10: Graphs

Connectivity

• A graph is connected if there is a path between every pair of vertices

• A connected component of a graph G is a connected subgraph of G

Connected graph

Non connected graph with two connected components

Page 11: Graphs

Connectivity

• A graph is complete if all pair of vertices are adjacent

Complete graphLet n = #vertices, and m = #edges

How many total edges in a complete graph?

Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice!!! Therefore, intuitively, m = n(n -1)/2.

Therefore, if a graph is not complete, m < n(n -1)/2

Page 12: Graphs

Trees and Forests• A forest is an undirected

graph without cycles• The connected

components of a forest are trees

• A (free) tree is an undirected graph T such that– T is connected– T has no cyclesThis definition of tree is

different from the one of a rooted tree Tree

Forest

Page 13: Graphs

Trees and Forests

Tree

Forest

Let n = #vertices, and m = #edges

How many total edges in a tree?

m = n - 1

Total edges in a forest? m < n - 1

Page 14: Graphs

Spanning Trees and Forests• A spanning tree of a

connected graph is a spanning subgraph that is a tree

• A spanning tree is not unique unless the graph is a tree

• A spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 15: Graphs

Data Structures for Graphs

• A Graph! How can we represent it?– Edge list

– Adjacency lists

– Adjacency matrix

Page 16: Graphs

Edge List• The edge list structure simply stores the vertices and the

edges into two containers (ex: lists, vectors etc..)• each edge object has references to the vertices it

connects.

DFWBOS ORDMIA SFOJFKLAX

DL 247 DL 335 UA 877NW 35 AA 523 AA 41 1 TW 45UA 120AA 49 AA 903AA 1387

E

V

Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence. Time: O(m)

Easy to implement.

Space = O(n+m)

Page 17: Graphs

Adjacency List (traditional)• adjacency list of a vertex v:

sequence of vertices adjacent to v• represent the graph by the adjacency lists of all the vertices

a b

c

d e

Space = (n + deg(v)) = (n + m)

b

b

c

c

c

d

a e

a d e

a e

d

b

a

c

d

e

Page 18: Graphs

Adjacency List (modern)• The adjacency list structure extends the edge list structure by

adding incidence containers to each vertex.

space is O(n + m).

in out in out in out in out in out in out in out

NW 35

DL 247

AA 49

AA 41 1

UA 120 AA1387

AA 523

UA 877

DL335

AA 49

NW 35 AA1387

AA 903

TW 45

DL 247

AA 903

AA523

AA 41 1

UA 120

DL 335

UA 877 TW 45

DFWBOS ORDMIA SFOJFKLAX

DL 247 DL 335 UA 877NW 35 AA 523 AA 41 1 TW 45UA 120AA 49 AA 903AA 1387

E

V

Page 19: Graphs

Adjacency Matrix (traditional)

• matrix M with entries for all pairs of vertices• M[i,j] = true means that there is an edge (i,j) in the graph.• M[i,j] = false means that there is no edge (i,j) in the graph.• There is an entry for every possible edge, therefore:

Space = (n2)

d

F T T T FT F F F TT F F T TT F T F TF T T T F

a b

c

e

a b c d eabcde

d

Page 20: Graphs

Adjacency Matrix (modern)• The adjacency matrix structures augments the edge list

structure with a matrix where each row and column corresponds to a vertex.

Page 21: Graphs

Data Structures for Graphs

• A Graph! How can we represent it?– Edge list

– Adjacency lists

– Adjacency matrix

Page 22: Graphs

Edge List• The edge list structure simply stores the vertices and the

edges into two containers (ex: lists, vectors etc..)• each edge object has references to the vertices it

connects.

DFWBOS ORDMIA SFOJFKLAX

DL 247 DL 335 UA 877NW 35 AA 523 AA 41 1 TW 45UA 120AA 49 AA 903AA 1387

E

V

Finding the edges incident on a given vertex is inefficient since it requires examining the entire edge sequence. Time: O(m)

Easy to implement.

Space = O(n+m)

Page 23: Graphs

Adjacency List• The adjacency list structure extends the edge list structure by

adding incidence containers to each vertex.

space is O(n + m).

in out in out in out in out in out in out in out

NW 35

DL 247

AA 49

AA 41 1

UA 120 AA1387

AA 523

UA 877

DL335

AA 49

NW 35 AA1387

AA 903

TW 45

DL 247

AA 903

AA523

AA 41 1

UA 120

DL 335

UA 877 TW 45

DFWBOS ORDMIA SFOJFKLAX

DL 247 DL 335 UA 877NW 35 AA 523 AA 41 1 TW 45UA 120AA 49 AA 903AA 1387

E

V

Page 24: Graphs

Adjacency Matrix• The adjacency matrix structures augments the edge list

structure with a matrix where each row and column corresponds to a vertex.

Page 25: Graphs

Graph Traversal

• A procedure for exploring a graph by examining all of its vertices and edges.

• Two different techniques:– Depth First traversal (DFT)– Breadth First Traversal (BFT)

Page 26: Graphs

• Depth-first search (DFS) is a general technique for traversing a graph

• A DFS traversal of a graph G – Visits all the vertices and edges of G– Determines whether G is connected– Computes the connected components of G– Computes a spanning forest of G

Depth-First Search

Page 27: Graphs

DB

A

C

E

Example

DB

A

C

E

DB

A

C

E

DB

A

C

E

discovery edgeback edge

A visited vertex

A unexplored vertex

unexplored edge

Page 28: Graphs

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

Page 29: Graphs

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 opposite(v,e)if getLabel(w)

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

elsesetLabel(e, BACK)

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

as discovery edges and back edgesfor all u G.vertices()

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

setLabel(e, UNEXPLORED)for all v G.vertices()

if getLabel(v) UNEXPLORED

DFS(G, v)

Page 30: Graphs

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 31: Graphs

Analysis of DFS

• Setting/getting a vertex/edge label takes O(1) time

• Each vertex is labeled twice – once as UNEXPLORED– once as VISITED

• Each edge is labeled twice– once as UNEXPLORED– once as DISCOVERY or BACK

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

Page 32: Graphs

Depth-First Search

• DFS on a graph with n vertices and m edges takes O(nm ) time

• DFS can be further extended to solve other graph problems– Find and report a path between two

given vertices– Find a cycle in the graph

Page 33: Graphs

Path Finding• We call DFS(G, u) with u as the start

vertex

• We use a stack S to keep track of the path between the start vertex and the current vertex

• As soon as destination vertex v is encountered, we return the path as the contents of the stack

Page 34: Graphs

Path FindingAlgorithm pathDFS(G, v, z)

setLabel(v, VISITED)S.push(v)if v z

return S.elements()for all e G.incidentEdges(v)

if getLabel(e) UNEXPLOREDw opposite(v,e)if getLabel(w) UNEXPLORED

setLabel(e, DISCOVERY)S.push(e)pathDFS(G, w, z)S.pop(e)

else setLabel(e, BACK)

S.pop(v)

Page 35: Graphs

Cycle Finding

• We use a stack S to keep track of the path between the start vertex and the current vertex

• As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w

Page 36: Graphs

Cycle FindingAlgorithm cycleDFS(G, v, z)

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

if getLabel(e) UNEXPLOREDw opposite(v,e)S.push(e)if getLabel(w) UNEXPLORED

setLabel(e, DISCOVERY)pathDFS(G, w, z)S.pop(e)

elseT new empty stackrepeat

o S.pop()T.push(o)

until o wreturn T.elements()

S.pop(v)

Page 37: Graphs

Review: Representation

a b

c

d e

Space = (n + deg(v)) = (n + m)

a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 38: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 39: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 40: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 41: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 42: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 43: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 44: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 45: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 46: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 47: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 48: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 49: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 50: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 51: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 52: Graphs

Review: DFS

a b

c

d e a b c d e

pa-b

pa-c

pa-d

pa-b

pb-e

pa-c

pc-d

pc-e

pa-d

pc-d

pd-e

pb-e

pc-e

pd-e

a-bpa | pb

a-cpa | pc

a-dpa | pd

b-epb | pe

c-dpc | pd

c-epc | pe

d-epd | pe

Page 53: Graphs

Breadth-First Search

CB

A

E

D

L0

L1

FL2

Page 54: Graphs

Example

discovery edgecross edge

A visited vertex

A unexplored vertex

unexplored edge

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

F

Page 55: Graphs

Example (cont.)

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 56: Graphs

Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 57: Graphs

BFS Algorithm• The algorithm uses a

mechanism for setting and getting “labels” of vertices and edges

Algorithm BFS(G, s)L new empty queueL.enqueue(s)setLabel(s, VISITED)while L.isEmpty()

v L.dequeue() for all e G.incidentEdges(v)

if getLabel(e) UNEXPLOREDw opposite(v,e)if getLabel(w)

UNEXPLOREDsetLabel(e, DISCOVERY)setLabel(w, VISITED)L.enqueue(w)

elsesetLabel(e, CROSS)

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

and partition of the vertices of G

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

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

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

UNEXPLORED

BFS(G, v)

Page 58: Graphs

PropertiesNotation

Gs: connected component of s

Property 1BFS(G, s) visits all the vertices and edges of Gs

Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs

CB

A

E

D

F

CB

A

E

D

F

Page 59: Graphs

Analysis

• Setting/getting a vertex/edge label takes O(1) time• Each vertex is labeled twice

– once as UNEXPLORED– once as VISITED

• Each edge is labeled twice– once as UNEXPLORED– once as DISCOVERY or CROSS

• Each vertex is inserted once into a queue L• Method incidentEdges is called once for each vertex• BFS runs in O(n m) time provided the graph is

represented by the adjacency list structure

Page 60: Graphs

Applications• we can specialize the BFS traversal of a

graph G to solve the following problems in O(n m) time– Compute the connected components of G– Compute a spanning forest of G– Find a simple cycle in G, or report that G is a forest– Given two vertices of G, find a path in G between

them with the minimum number of edges, or report that no such path exists

Page 61: Graphs

DFS vs. BFSBack edge (v,w)

– w is an ancestor of v in the tree of discovery edges

Cross edge (v,w)– w is in the same level as

v or in the next level in the tree of discovery edges

CB

A

E

D

F

CB

A

E

D

F

DFS BFS

Page 62: Graphs

DFS vs. BFS (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS

Applications DFS BFSSpanning forest, connected components, paths, cycles

Shortest paths

Biconnected components

Page 63: Graphs

Quiz 101. Let G be a graph whose vertices are the integers 1 through 8, and let

the adjacent vertices of each vertex be given by the table.

Assume that, in a traversal of G, the adjacent vertices of a given vertex are returned in the same order as they are listed in the above table.

a) Draw G

b) Order the vertices as they are visited in a DFS traversal starting at vertex 1.

c) Order the vertices as they are visited in a BFS traversal starting at vertex 1.

Vertex Adjacent vertices

1

2

3

4

5

6

7

8

(2,3,4)

(1,3,4)

(1,2,4)

(1,2,3,6)

(6,7,8)

(4,5,7)

(5,6,8)

(5,7)

Page 64: Graphs

Quiz 10(Contd.)

2. Would you use the adjacency list structure or the adjacency matrix structure in each of the following cases? Justify your choice.

a) The graph has 10,000 vertices and 20,000 edges, and it is important to use as little space as possible

b) The graph has 10,000 vertices and 20,000,000 edges, and it is important to use as little space as possible

c) You need to answer the query areAdjacent as fast as possible no matter how much space you use.

Page 65: Graphs

Directed Graphs

JFK

BOS

MIA

ORD

LAXDFW

SFO

Page 66: Graphs

Digraphs

• A digraph is a graph whose edges are all directed– Short for “directed graph”

• Applications– one-way streets– flights– task scheduling A

C

E

B

D

Page 67: Graphs

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 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 68: Graphs

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

• 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 69: Graphs

COT 4210(Discrete II)

COP 4020(Prog. Langs.)

COT 3960

COP 4710(Database Sys.)

COP 4600(Operating Sys.)

CEN 5016(Software Eng.)

CDA 4150(Comp. Arch.)

COP 3502(CS1)

COP 3503(CS2)

COP 3530(CS3)

COP 3330(OOP & UML)

COP 3223(C – Progmng)

COP 3402(Systems SW)

COT 3100(Discrete I)

CDA 3103(Comp. Org.)

COP 4232(Software Syst.Develop.)

Recommended Pre-reqPrereq

MAC 2312(Calc II)

Java

FoundationExam

C

C

Java

Java

C++Ada

C++SQL

C

COT 4810(Topics in CS)

 

Computer Science Pre-requisite Graph• edge (a,b) means task a must be completed before b can be started

Page 70: Graphs

COT 4210(Discrete II)

COP 4020(Prog. Langs.)

COT 3960

COP 4710(Database Sys.)

COP 4600(Operating Sys.)

CEN 5016(Software Eng.)

CDA 4150(Comp. Arch.)

COP 3502(CS1)

COP 3503(CS2)

COP 3530(CS3)

COP 3330(OOP & UML)

COP 3223(C – Progmng)

COP 3402(Systems SW)

COT 3100(Discrete I)

CDA 3103(Comp. Org.)

COP 4232(Software Syst.Develop.)

Recommended Pre-reqPrereq

MAC 2312(Calc II)

Java

FoundationExam

C

C

Java

Java

C++Ada

C++SQL

C

COT 4810(Topics in CS)

 

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

1

2

3

4 5

6 78

910 11

12 13 14 15 16 17 18

Page 71: Graphs

Topological Sorting Example

Strategy: Pick up a vertex which has zero incident edges.

Page 72: Graphs

Topological Sorting Example

00

1 32

2

31

1

Graph with # of Incident edges for each vertex.

Page 73: Graphs

Topological Sorting Example

100

1 32

2

31

1

Page 74: Graphs

Topological Sorting Example

10

1 21

2

31

1

Page 75: Graphs

Topological Sorting Example

2 10

1 21

2

31

1

Page 76: Graphs

Topological Sorting Example

2 1

0 11

2

31

1

Page 77: Graphs

Topological Sorting Example

2 1

30 11

2

31

1

Page 78: Graphs

Topological Sorting Example

2 1

3 01

2

30

0

Page 79: Graphs

Topological Sorting Example

2

4

1

3 01

2

30

0

Page 80: Graphs

Topological Sorting Example

2

4

1

3

0

2

30

0

Page 81: Graphs

Topological Sorting Example

2

4

5

1

3

0

2

30

0

Page 82: Graphs

Topological Sorting Example

2

4

5

1

3

1

20

0

Page 83: Graphs

Topological Sorting Example

2

4

56

1

3

1

20

0

Page 84: Graphs

Topological Sorting Example

2

7

4

56

1

3

0

20

Page 85: Graphs

Topological Sorting Example

2

7

4

56

1

3

10

Page 86: Graphs

Topological Sorting Example

2

7

4

8

56

1

3

10

Page 87: Graphs

Topological Sorting Example

2

7

4

8

56

1

3

0

Page 88: Graphs

Topological Sorting Example

2

7

4

8

56

1

3

9

Page 89: Graphs

Algorithm for Topological SortingAlgorithm TopologicalSort(G) Let S be an empty stack for each vertex u of G do set its in_counter

if in_counter = 0 then Push u in S

i 1 while S is not empty do

Pop v from S Label v ii i + 1

for every w adjacent to v do reduce the in_counter of w by 1 if in_counter = 0 then

Push w in S if (i < # of vertices)

“Diagraph has a directed cycle”

Run Time: O(n+m)

Space use: O(n)

Page 90: Graphs

Programming Assignment 3

• Implement a weighted graph ADT using adjacency list data structure with the following methods.– DFS– BFS– Dijkstra’s Shortest Path finding algorithm– Kruskal’s Minimum Spanning Tree

algorithm