graphs
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 PresentationTRANSCRIPT
![Page 1: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/1.jpg)
Graphs
![Page 2: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/2.jpg)
Graphs are not function plots.
1stQtr
2ndQtr
3rdQtr
4thQtr
East
North0
20
40
60
80
100
East
West
North
![Page 3: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/15.jpg)
Data Structures for Graphs
• A Graph! How can we represent it?– Edge list
– Adjacency lists
– Adjacency matrix
![Page 16: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/21.jpg)
Data Structures for Graphs
• A Graph! How can we represent it?– Edge list
– Adjacency lists
– Adjacency matrix
![Page 22: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/26.jpg)
• 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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/28.jpg)
Example (cont.)
DB
A
C
E
DB
A
C
E
DB
A
C
E
DB
A
C
E
![Page 29: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/42.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/43.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/44.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/45.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/46.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/47.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/48.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/49.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/50.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/51.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/52.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/53.jpg)
Breadth-First Search
CB
A
E
D
L0
L1
FL2
![Page 54: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/54.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/55.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/56.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/57.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/58.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/59.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/60.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/61.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/62.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/63.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/64.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/65.jpg)
Directed Graphs
JFK
BOS
MIA
ORD
LAXDFW
SFO
![Page 66: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/66.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/67.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/68.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/69.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/70.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/71.jpg)
Topological Sorting Example
Strategy: Pick up a vertex which has zero incident edges.
![Page 72: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/72.jpg)
Topological Sorting Example
00
1 32
2
31
1
Graph with # of Incident edges for each vertex.
![Page 73: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/73.jpg)
Topological Sorting Example
100
1 32
2
31
1
![Page 74: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/74.jpg)
Topological Sorting Example
10
1 21
2
31
1
![Page 75: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/75.jpg)
Topological Sorting Example
2 10
1 21
2
31
1
![Page 76: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/76.jpg)
Topological Sorting Example
2 1
0 11
2
31
1
![Page 77: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/77.jpg)
Topological Sorting Example
2 1
30 11
2
31
1
![Page 78: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/78.jpg)
Topological Sorting Example
2 1
3 01
2
30
0
![Page 79: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/79.jpg)
Topological Sorting Example
2
4
1
3 01
2
30
0
![Page 80: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/80.jpg)
Topological Sorting Example
2
4
1
3
0
2
30
0
![Page 81: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/81.jpg)
Topological Sorting Example
2
4
5
1
3
0
2
30
0
![Page 82: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/82.jpg)
Topological Sorting Example
2
4
5
1
3
1
20
0
![Page 83: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/83.jpg)
Topological Sorting Example
2
4
56
1
3
1
20
0
![Page 84: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/84.jpg)
Topological Sorting Example
2
7
4
56
1
3
0
20
![Page 85: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/85.jpg)
Topological Sorting Example
2
7
4
56
1
3
10
![Page 86: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/86.jpg)
Topological Sorting Example
2
7
4
8
56
1
3
10
![Page 87: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/87.jpg)
Topological Sorting Example
2
7
4
8
56
1
3
0
![Page 88: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/88.jpg)
Topological Sorting Example
2
7
4
8
56
1
3
9
![Page 89: Graphs](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/89.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062519/568150b7550346895dbed07d/html5/thumbnails/90.jpg)
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