A Graphis a non-linear data
structure consisting of nodes and
edges.
The set of edges describes
relationships among the vertices.
So, A Graph is a finite set of
vertices(or nodes) and set of Edges
which connect a pair of nodes.
Graphs
2.
A graphG is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of
vertices
E(G): a set of edges (pairs of
vertices)
Graphs
3.
Example : GraphG
Graphs
A B C
D E
Vertices V(G) = {A, B, C , D, E}
Edges E(G) = { (A,B), (A,D), (B,C), (B,D), (C,E),
(D,E)}
4.
According to Pathor Flow:
Undirected: In a graph if edges
have no direction then it is called
Undirected Graph.
Directed: In a graph if edges have
direction then it is called Directed
Graph.
Graphs Types
5.
Graphs Types
A BC
D E Undirected Graph
Vertices V(G) = {A, B, C , D, E}
Edges E(G) = { (A,B), (A,D), (B,C), (B,D), (C,E),
(D,E)}
6.
Graphs Types
Directed Graph
A
BC
D E
Vertices V(G) = {A, B, C , D, E}
Edges E(G) = { (A,B), (A,D), (B,C), (D,B), (D,E),
(E,C)}
7.
Graph Terminology
Adjacent Nodes:Two nodes are
adjacent if they are connected
with an edge.
A is Adjacent to B
B is adjacent From A
Path: A sequence of vertices
that connect two nodes in a
graph.
A B
8.
Graph Terminology
Complete Graph:A graph in
which every vertex is directly
connected to every other
vertex.
if Number of vertices (v) are n then
Number of edges (e) are
In Directed Graph e=n*(n-1)
In Undirected Graph e=(n*(n-1))/2
9.
Graph Terminology
Complete Graph:
AB
C
Directed Complete Graph
e= n*(n-1)= 3*2 = 6
Vertices V(G) = {A, B, C }
Edges E(G) = {(A,B), (A,C), (B,A), (B,C), (C,A),
(C,B)}
10.
Graph Terminology
Complete Graph:
AB
C
Undirected Complete Graph
e= (n*(n-1))/2= 6/2=3
Vertices V(G) = {A, B, C }
Edges E(G) = {(A,B), (A,C), (B,C)}
Graph Terminology
Loop: Ingraph loop (also called
a self-loop) is an edge that
connects a vertex to itself. A
simple graph contains no
loops..
A B C
D E
In this graph vertex C having
loop
13.
Graph Terminology
Graph Degree:
Degreeof the Vertex
In-Degree of the vertex
Out-Degree of the vertex
In this Degree works on
undirected Graph
In-Degree and Out-Degree works
on Directed Graph
14.
Graph Terminology
Degree: thenumber of
relations a particular node /
Vertex makes with the other
node / Vertex in the graph.
d(v).
In this graph at vertex D out degree is d(D) =
3
A B C
D E
15.
Graph Terminology
Out-Degree: ifV is vertex of a
directed graph D number of
edges for which v is the initial
vertex is called out- degree. It is
denoted by d+
(v).
A B
D
In this graph at vertex A out degree is d+
(A) =
2
16.
Graph Terminology
In-Degree: ifV is vertex of a
directed graph D number of
edges for which v is the
terminal vertex is called in
degree. It is denoted by d-
(v).
A B
D
In this graph at vertex D in degree is d-
(D) = 1
17.
Graph Terminology
Note1: inan undirected graph
Degree of total Graph is sum of
all the vertex degrees.
d(G)=∑ d(v)
Note2: in a directed graph
Degree of total Graph is sum of
in-degree and out-degree.
d(G)=∑ d+
(v) + ∑ d-
(v)
18.
Graph Terminology
Note3: allvertex in-degree and
out-degree always same.
∑ d+
(v) = ∑ d-
(v)
Note4: in a graph Total number
of edges is equal to the half of
the tree degree.
e= ∑ d(v) / 2
19.
Graph Terminology
Note5: ina Directed graph Total
number of edges is equal to all vertex
in-degree or out-degree.
e = ∑ d+
(v) = ∑ d-
(v)
Note6: Self loop can be treated as 1
outer and one inner so total 2.
d+
(v) = d-
(v) = 1
C
20.
Graph Terminology
A d(A)= 2
vertex Degree
B d(B) = 3
C d(C) = 2
D d(D) = 3
E d(E) =
2
A B C
D E
Vertex (v)=5
edges (e)=6
e=∑ d(v)/2
∑ d(v)=2*e
∑ d(v)=2*6=12
∑ d(v)=2+3+2+3+2=12
21.
Graph Terminology
A BC
D E
A d+
(A) = 2 d-
(A) = 0
vertex Out-Degree In-Degree
B d+
(B) = 1 d-
(B) = 2
C d+
(C) = 0 d-
(C) = 2
D d+
(D) = 2 d-
(D) = 1
E d+
(E) = 1 d-
(E) = 1
e=∑ d+
(v)=∑ d-
(v)
∑ d+
(v)=2+1+0+2+1=6 ∑ d-
(v)=0+2+2+1+1=6
Vertex (v)=5
edges (e)=6
22.
Graph Terminology
A BC
D E
A d+
(A) = 2 d-
(A) = 0
vertex Out-Degree In-Degree
B d+
(B) = 1 d-
(B) = 2
C d+
(C) = 1 d-
(C) = 3
D d+
(D) = 2 d-
(D) = 1
E d+
(E) = 1 d-
(E) = 1
e=∑ d+
(v)=∑ d-
(v)
∑ d+
(v)=2+1+1+2+1=7 ∑ d-
(v)=0+2+3+1+1=7
Vertex (v)=5
edges (e)=7
23.
Tree Vs Graph
InTree only path allowed
between nodes but in Graph
nodes can have directional or
unidirectional.
In Tree each node having only
one parent but in Graph there is
no such concept.
In tree there is no concept loops
and self loops unlike graphs.
24.
Tree Vs Graph
Treeuses traversed using in-
order, pre-order, post-order but
Graph uses BFS(Breadth First
Search) and DFS(Depth First
Search).
Tree is a hierarchical structure
and Graph has a network
model.