




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to graphs, their terminology, applications, and various representations. It covers undirected and directed graphs, their differences, and graph traversals such as depth first search (dfs) and breadth first search (bfs). The document also includes examples and pseudo-code for graph traversals.
Typology: Slides
1 / 8
This page cannot be seen from the preview
Don't miss anything!





gy,
b^ V= {a,b,c,d,e}^ E= {(a,b),(a,c),(a,d),(b^
) (^ d) (^ ) c^
(b,e),(c,d),(c,e),(d,e)} d^
e
CS
electronic circuits
CS
networks (roads, flights,communications)
karachi Islamabad^ LAX
Abbott Lahore
Maree Lahore
Muza-rabad
0 v^1 ) is incident on vertices v
0 and v^1
The edge (v^0
, v^1 ) is incident on vertices v
0 and v^1
If <v^0 , v^1 > is an edge in a directed graphi^ dj^
t t^ d^
i^ dj^ t f v^0 is adjacent to v
1 , and v^1 is adjacent from v
0
The edge <v^0
, v^1 > is incident on v
0 and v^1 docsity.com
d^ h For directed graph,^ the in-degree of a vertex
v^ is the number of edges that have^ v^ as the headthat have^ v^ as the head the out-degree of a vertex
v^ is the number of edges that have^ v^ as the tail
3
2
3 3
directed graph 30 in:1, out: 1 in-degreeout-degree^
in: 1, out: 2 1 in: 1, out: 0 (^2) G 3
f path:^ sequence ofvertices v^ ,v^1
,.. .vsuch k^ that consecutive
3
2
that consecutive vertices v^ and vi^
arei+ adjacent.
3
j
3
3 a^
a^ b
b c ed
c ed 7 d^
e^ d^
e a b e d c^
b e d c
2 3
6 7 4 5 Legend^
8
Directional path
Bi-directional paths
docsity.com
A graph where edges are directedA graph where edges are directed
An^ undirected graph
is one in which the pair An^ undirected graph
is one in which the pair of vertices in a edge is unordered, (v
0 , v^1 ) =
(v^1 v^0 )(v^1 ,v^0 ) A directed graph is one in which each edge isa directed pair of vertices <v
v > !=
0 , v^1 > != <v
1 ,v^0 > t il^
h^ d tail^
head
i^ f^ i^
di^ i^
l
The adjacency matrix of G is a two-dimensionaln by n array, say adj_mat If the edge (v
i^ , vj^ ) is in E(G), adj_mat[i][j]= If there is no such edge in E(G), adj
mat[i][j]=
t e e s^ o suc
edge^
(G), adj_^
at[ ][j]^0
The adjacency matrix for an undirected graph issymmetric; the adjacency matrix for a digraphsymmetric; the adjacency matrix for a digraphneed not be symmetric
docsity.com
Examples for Adjacency Matrixp^
f^ j^
y (^0 01 23 10111) ⎡⎤ ⎢⎥ (^1011) ⎢⎥ ⎢⎥
(^010) ⎡⎤ ⎢⎥ (^101) ⎢⎥ ⎢⎥ (^3 12) ⎢⎥ (^1101) ⎢ ⎥^1110 ⎣⎦
(^40512)
1 2 3
3
(^67)
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
l^ th^ t^ ti
d^ d^
i^ h
Start several paths at a time, and advance in eachone step at a time
B^ A C^ D B^ A C^ D F^ E G^ H I^ J^
K^ L M^ N^
O^ P M^ N^
O^ P
docsity.com
3 0
1 2
3
B^ A C^ D^
A^ B^
C^ D
E^ F^
G^ H^4
E^ F^
G^ H^4
J^ I K^ L^
I^ J^
K^ L
M^ N^
O^ P^
M^ N^
O^ P^5
t^ t i^
t initialize container L
to contain vertex s 0 i^ ←^^0 while L^ is not empty doi^ create container L
to initially be emptyi+ for each vertex
v^ in Ldoi^ if edge^ e^ incident on v dog let^ w^ be the other endpoint of
e if vertex w is unexplored thenlabel^ e^ as a discovery edgelabel^ e^ as a discovery edgeinsert^ w^ into Li+1else label e as a cross edgei i
26
i^ ←^ i^ + 1
Find path from source vertex s to destinationdvertex d Use graph search starting at s and terminating as soon as we reach d^ Need to remember edges traversed Use depth – first search? Use breath – first search?Use breath
first search?
F^ B^
A^ start
DFS Process
G^ DC destinationDFS on CC DFS on BBB
Call DFS on DD Return to call on BB
DFS on A^ A A A
A C ll^ S^ G^
f^ d d^ ti^
ti^ d^! Call DFS on GG (^) D B
found destination - done!Path is implicitly stored in DFS recursionPath is: A, B, D, G B^ A
Path is: A, B, D, G
docsity.com
FB GD
A^ start
BFS Process^
G^ DC destinationfront rear^
frontrear
frontrear
frontrear
A Initial call to BFS on A
B Dequeue A frontrear
frontrear
frontrearCD (^) Dequeue B
frontrearD Dequeue C
Initial call to BFS on AAdd A to queue
Dequeue AAdd B
Dequeue BAdd C, D
Dequeue CNothing to add
frontrear Gfound destination - done!Dequeue DPath must be stored separatelyAdd G Add G
docsity.com