Graph Notes by Kapil Yadav
Graph Notes by Kapil Yadav
nunumun
(Part-1)
r
- By Kapil Yadav
edge I 3
t
vertex
Y
O 5
2 Y
v
20,1,2,3,4,53
=
t
220,1350,2331,3322,4333,5334,533
=
Order of Graphi-
of of
->
The number vertices in a
graph represents order
graph.
o(h) G
=
size of Graph: -
No.
edges in
graph
-
of a
Size (a) = e =
6
Types of Graph:
-
Simple Graph
undirected Multi
Graph
Graph Pseudo-Graph
Graph
Simple Directed Graph
Directed Multi Graph
graph Pseudo Graph
https://www.linkedin.com/in/kapilyadav22/
1) Undirected
Graph:-Edges in Undirected graph are
Bidirectional-
I 3
O 5
2 Y
undirected graph
directed
2. Directed
Graph:-Graph having edges.
edge I >3
T
t
vertex
Y
S
O 5
T
s
2 >Y
Simple Graph: -
No
->
self loops.
No
Multiple edges.
- >
Multi
Graphi-
allowed
-
Multiple edges are -
pseudo Graph:
-
self allowed
->
loops are
Degree of a vertex: -
->
Number Vertices vertex
of adjacent to a V.
https://www.linkedin.com/in/kapilyadav22/
I 3
O 5
24
deg(2) 2
=
of Directed
-> Incase
graph, each vertex has an
indegree
and an
outdegree.
T
I >3
S
O 5
T
I
2 >Y
Indegree (2) 1
=
Outdegree (2) I
=
Path.-Path is of and
a
sequence alternating vertices edges
such that the
edge connects each successive Vertex
I 2
Y 3
S
Path =
1 2 345
https://www.linkedin.com/in/kapilyadav22/
starts
Cycle: Cycle is a
path that and ends at
the same vertex.
I 2
12341
3 4123
↑
Y 3
4123Y
Tree is connected
graph with
cycles.
->
a no
Graph Representation: -
Adjacency Matrix
1.
list
2.
Adjacency (when edges
3. Incidence Matrix are labelled)
1.
Adjacency Matrix:-A
graph a with vertices,
UXU matrix
can be
represent using
911 912...... 91
i
anc anz----and
->
undirected the
In
graph, value
aij=aji,
and the
adjacency matrix is
symmetric.
I 2 3 Y
I
2
I O I O I
2 I O I I
3 01 01
3
Y I
Y 1 I O
sc 0(vxv)
=
https://www.linkedin.com/in/kapilyadav22/
Adjacency Matrix of Undirected
Graph
line
->
for Directed
graph, remove 15.
https://www.linkedin.com/in/kapilyadav22/
list:
2.
Adjacency
-
We use
of
vector to
represent the graph
->
an
array
Vertex
7 2 Y
I 2
2 3 I
3 2
Y
Y 3
Y I 3
list
Adjacency
Space 0(2E)
=
Adjacency
list of Undirected
Graph
line
->
for Directed
graph, remove 13.
https://www.linkedin.com/in/kapilyadav22/
Connected Components: of
No. connected
subgraph in a
-
given graph.
* *
* g
-
of components
Number in connected graph =1
https://www.linkedin.com/in/kapilyadav22/
Breadth-First Search: BEs is one
of the traversal
techniques.
->
There can be
multiple connected components, so we need
to have visited
Each
*
node will be
array.
either visited or not visited.
BES the
graph layerwise, Quere
-> traverse so we use
Algorithm:
-
and
starting
·
Take insert the node.
a
queue
visited with
Declare and
initialize it
·
array
a
O X
visited.
Mark the
starting vertex as
·
Follow the
steps, until the queue become
empty.
·
ple1:
I 3
starting
Node
t
O 5 6
C
2 Y
ooo
visited O 00 O
D ↑ 2 3456
queue
1. Push O in
queue
visited D O 000 0 O
D 12345 6
O
https://www.linkedin.com/in/kapilyadav22/
queue
While does not
2.
queue get empty,
W
Pop 0, Push 1, Push 2
visited D I I O 00 O
att
D I 2 3 45 6
3)Pop 1, Push 3
visited D I I I 000
D I 2 3 45 6
4) Pop 2, Push
Y
visited
D
D
I
L
23
I L 1
45
0 O
6 W&
Y
3
S.) Pop
visited
3
I
D
Push
L
S
I L I I O
quarte
S
D I 23 Y S 6 B
queue
6)
Pop 4
D L I L I I S
visited O
D I 2 3 Y S 6 M
queue
7) Pop 5, Push 6
visited D L I L I I
I 6
D I 2 3 Y S 6 I
queue and
8) 6 f
Pop
20, 1, 2, 3,
visited D L I h 1 I I
4,5,63
D I 23 45 6 ⑯
https://www.linkedin.com/in/kapilyadav22/ queue
BES
using Adjacency Matrix (Undirected Graph)
0Cvxv)
Tc =
s 0(uXv)
= + o(v)
to t
Adj matrix to store answer
https://www.linkedin.com/in/kapilyadav22/
O I 23Y S 6
O
O O I I O 00
O
I O O
I
· O O
O
I O
ai
O O O
i
O I O O O O
O O
O O I O
. 00 I L O I
O O
O O O
I O
https://www.linkedin.com/in/kapilyadav22/
BES
Using Adjacency List (Undirected Graph)
o(V) o(z)
xc
=
No.
of
Rotal degrees
vertices
sc 0(v
=
+ t)
=
o(v?) [In Worst Case]
https://www.linkedin.com/in/kapilyadav22/
O I 2
O
7 3
2 O Y
3 I S
Y 2 S
S 3 u 6
6 S
list
Adjacency
https://www.linkedin.com/in/kapilyadav22/
Depth First Search (DFS):
Depth first search is a recursive
algorithm
starting
In DFS, select node, and it
explore
->
we a
as far as
possible.
Algorithm:
Choose
starting node and do recursion.
·
a
·
Make current mode as visited and add current node in
our
output list
(vector/away).
and unvisited
Traverse all the
adjacent nodes.
·
Ex- I 3
starting
Node
t
O 5 6
C
2 Y
dfs(o)
visited
-
dfs(I) dfs(z)
dfs(3)
dfs(5)
df(u) dfs(c)
dfs(z) https://www.linkedin.com/in/kapilyadav22/
https://www.linkedin.com/in/kapilyadav22/
https://www.linkedin.com/in/kapilyadav22/