You are on page 1of 25

Fariha Tabassum Islam

GRAPH Courtesy:
Dr. Md. Abul Kashem Mia, Sheikh
Adilina
07/24/2021 CSE 217/ CSE 2215 1
WHAT IS A GRAPH?
A graph is a pair (V, E), where
 V is a set of nodes, called vertices
 E is a collection of pairs of vertices, called
edges

07/24/2021 CSE 217/ CSE 2215 2


APPLICATIONS:
Google Maps Find a path from one place to another
Social Media Find the shortest path from one place to
Any kind of network another
Telephone network Determine connectivity
Transportation
Circuit
Internet
Game
Finance

07/24/2021 CSE 217/ CSE 2215 3


07/24/2021 CSE 217/ CSE 2215 4
07/24/2021 CSE 217/ CSE 2215 5
directed unweighted graph
graph
𝑎

1
  undirected

GRAPH TERMINOLOGY:

5
0
b
graph
unweighted
c
graph

2
 
Weighted and Unweighted cyclic

3
d e
Directed and Undirected acyclic graph
graph

4
Cyclic and Acyclic
1 3

1
Adjacent Vertex

5
0
 b and c are adjacent vertex of
4 5 weighted graph
Edges incident to a vertex 1
 (,b) and (,c) are incident to

2
cyclic 4 directed

3
Degree of a vertex graph 5
 degree of a = 2 graph
 Indegree

4
 Outdegree

Anomalies – Self loop, Parallel/Multi


edges
Disconnected graph

07/24/2021 CSE 217/ CSE 2215 6


directed unweighted graph
graph
𝑎

1
  undirected

GRAPH TERMINOLOGY:

5
0
b
graph
unweighted
c
graph

2
• Path cyclic

3
d e
• a–c-e acyclic graph
• d- b – a – c –e graph

4
1 3

1
• a – c – b- a – c –e
• Simple path

5
0
4 5 weighted graph
• Cycle 1
• a – c –b – a

2
• Simple cycle cyclic 4 directed

3
graph 5 graph
• Outgoing edges of a vertex
• incoming edge of a: (b,a) and

4
(c,a)

• Incoming edges of a vertex


• outgoing edge of a : (a,b) and
(a,c)

07/24/2021 CSE 217/ CSE 2215 7


 the maximum number of edges in a undirected graph with number of nodes
 If self loop allowed
 If not
 If multi edge allowed
 If not 1

6
0
 The sum of degrees of nodes 2
 = 5*(5-1) = 20 5
7
the maximum number of edges in a directed graph
4 3
 The sum of in degree of n nodes =
 The sum of out degree of n nodes?

the maximum number of edges in a binary tree of nodes =


A graph has 5 nodes and 16 edges. This is a undirected graph. Is it a multiedge graph?

07/24/2021 CSE 217/ CSE 2215 8


GRAPH
REPRESENTATION
07/24/2021 CSE 217/ CSE 2215 9
Can we represent self loop with adjacent matrix?
Can we represent self loop with adjacent list?

Can we represent parallel edges with adjacent matrix?


Can we represent parallel edges with adjacent list?

How many vertices are there in the graph?


How many edges are there in the graph?

Is the graph directed or undirected? connected or disconnected? weighted or unweighted?


Does the graph contains self loop or multi-edge?

07/24/2021 CSE 217/ CSE 2215 10


SIMPLE GRAPH
No self loop
No multi edges

07/24/2021 CSE 217/ CSE 2215 11


GRAPH REPRESENTATION
Adjacency Matrix representation
Adjacency list representation

07/24/2021 CSE 217/ CSE 2215 12


GRAPH REPRESENTATION
Adjacency Matrix representation
Adjacency list representation

Undirected Graph

07/24/2021 CSE 217/ CSE 2215 13


GRAPH REPRESENTATION
Adjacency Matrix representation
Adjacency list representation

Directed Graph

07/24/2021 CSE 217/ CSE 2215 14


ADJACENCY MATRIX
REPRESENTATION

weighted graph
unweighted graph

07/24/2021 CSE 217/ CSE 2215 15


ADJACENCY MATRIX
REPRESENTATION
0 1 2 3 4 5

0 1 1
1

1
5
0

2 1 1 1
2

3
3

4
4

5 1

07/24/2021 CSE 217/ CSE 2215 16


ADJACENCY LIST
REPRESENTATION Node{
int data;
Node* next;
}
1

5 0 1 2 /
0

1 3 /
2 4 3 1 /
2

3 /
4 /
4

5 1 /

07/24/2021 CSE 217/ CSE 2215 17


ADJACENCY LIST
REPRESENTATION Node{
int data;
int weight;
1 3 Node* next;
1

}
5
0

4 5
1 0 1|1 2|1 /
1 3|5 /
4
2

2 4|5 3|4 /
3

5
3 /
4

4 /
5 1|3 /

07/24/2021 CSE 217/ CSE 2215 18


DRAW THE GRAPH, DRAW ITS
ADJACENCY MATRIX AND ADJACENCY
LIST REPRESENTATION
Sample Input 2 Sample Input 2
5 Total number of vertices 5 Total number of vertices
5 Total number of edges 5 Total number of edges
0, 3 0, 3, 32
0, 2 0, 2, 234
0, 1 0,1, 342
1, 2 1, 2, 465
2, 4 2, 4, 64

07/24/2021 CSE 217/ CSE 2215 19


1

0 1 2 3
0 4
2
1 1
0
3 4

1
Sample Input 2
5 Total number of vertices 2
5 Total number of edges
03 3
02
01
4
12
24

07/24/2021 CSE 217/ CSE 2215 20


EXERCISE: DRAW THE GRAPH, DRAW ITS
ADJACENCY MATRIX AND ADJACENCY
LIST REPRESENTATION
Sample Input 1
10 vertices
edges are:
05
02
01
14
13
25
37
49
48
56
89

07/24/2021 CSE 217/ CSE 2215 21


07/24/2021 CSE 217/ CSE 2215 22
07/24/2021 CSE 217/ CSE 2215 23
GRAPH TRAVERSAL
There are two standard graph traversal techniques:
 Breadth-First Search (BFS)
 Depth-First Search (DFS)

07/24/2021 CSE 217/ CSE 2215 24


07/24/2021 CSE 217/ CSE 2215 25

You might also like