Graph Data Structure
Graph Data Structure
and Applications
CT-157
Course Instructor
Engr. Vanesh Kumar
Graphs
Introduction
• Graphs represent the • Each node represents an item
relationships among data items • Each edge represents the
• A graph G consists of relationship between two items
• A set V of Nodes (vertices)
• A set E of Edges: each edge
connects two nodes
node
edge
Introduction
• Graphs are a generalization of trees
• Nodes or vertices
• Edges or arcs
A
• Two kinds of graphs B C
• Directed
• Undirected D E
F
Undirected graph G
Directed graph
Daily Life Examples of Graph
Molecular Structure Computer Network
H Server 1 Terminal 1
H C H
Terminal 2
H Server 2
Other examples: electrical and communication networks, airline routes, flow chart,
graphs for planning projects
Introduction: Formal Definition
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• Each edge is a pair (v,w) where v, w V
v
(v, w)
w
Example of Graph
a b
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c), (a,d), (b,e),(c,d),(c,e), (d,e)}
Introduction: Formal Definition
• A directed graph, or digraph, is a graph in which the
edges are ordered pairs
• (v, w) ≠ (w, v)
• An undirected graph is a graph in which the edges are
unordered pairs
• (v, w) == (w, v)
Introduction: Directed Graphs
• In a directed graph, the edges are arrows.
• Directed graphs show the flow from one node to another and not
vise versa.
Introduction: Undirected Graphs
• In a Undirected graph, the edges are lines.
• UnDirected graphs show a relationship between two
nodes.
Terminology
ontain any c
v
(u, v)
u
w
u and v are adjacent
v and w are not adjacent
Path and Simple path
• A path from v1 to vk is a sequence of nodes v1, v2, …, vk
that are connected by edges (v1, v2), (v2, v3), …, (vk-1, vk)
• A path is called a simple path if every node appears at most
once.
v2 v3
v1
v2
v1 v3
v2
v1 v3
v4 v5
v1 v3 v7 v8
v2
v4 v5
v6 v9
v2 v7 v8
v1 v3
v4 v5
v6 v9
Complete Graph
v2 v2
v1 v3 v3
v4 v5 v4 v5
G H
Complete Graph
A complete graph is a graph that has the maximum
number of edges
A V1
B
Isolated vertex
Pendent vertex
connected
acyclic.
Tree & Forest…
• tree - connected graph without cycles
• forest - collection of trees
tre e
tre e
forest
tree
tree
Isomorphic Graph
• Isomorphism
– Two graphs are isomorphic, if they are structurally identical,
which means that they correspond in all structural details.
– Formal vertex-to-vertex and edge–to-edge correspondence is
called isomorphism.
Vertices(A) : a b c d e
Vertices(B): q p r s t
Degree of 2 3 3 3 1
vertices:
Edges(A): e1 e2 e3 e4 e5 e6
Edges(B): e’1 e’4 e’3 e’2 e’5 e’6
Examples for non-isomorphic graphs
• Edge-labeled graph
If all the Edges in a graph are given a label then it is Edge-
labeled graph
Representation of Graph
Following are the representation of agraph
• Adjacency Matrix
• Represent a graph using a two-dimensional array
• Adjacency List
• Represent a graph using n linked lists where n is the number of
vertices
A B,C, D
B C
C
D C,E
E C
a) Graphs G
b) Adjacency lists of G
Adjacency matrix for directed graph
Matrix[i][j] = 1 if (vi, vj)E
0 if (vi, vj)E
1 2 3 4 5
v1 v2 v3 v4 v5
v2 1 v1 0 1 0 0 0
v1 v3
2 v2 0 0 0 1 0
3 v3 0 1 0 1 0
v4 v5 4 v4 0 0 0 0 0
G 5 v5 0 0 1 1 0
Adjacency matrix for weighted undirected graph
Matrix[i][j] = w(vi, vj) if (vi, vj)E or (vj, vi)E
∞ otherwise
1 2 3 4 5
v2 v1 v2 v 3 v4 v5
v1 2 v3
5 1 v1 ∞ 5 ∞ ∞ ∞
4 3
7 2 v2 5 ∞ 2 4 ∞
v4 8
v5 3 v3 0 2 ∞ 3 7
4 v4 ∞ 4 3 ∞ 8
G 5 v5 ∞ ∞ 7 8 ∞
Adjacency list for directed graph
v2 1 v1 v2
v1 v3
2 v2 v4
3 v3 v2 v4
v4 v5
4 v4
G 5 v5 v3 v4
Adjacency list for weighted undirected graph
v1
v2 1 v2(5)
v1 5 2 v3 2 v2 v1(5) v3(2) v4(4)
4 3 3 v3 v2(2) v4(3) v5(7)
7
4 v4 v2(4) v3(3) v5(8)
v4 8 v5
5 v5 v3(7) v4(8)
G
Pros & Cons
• Adjacency matrix
Allows us to determine whether there is an edge from node i to node j in O(1) time
Adjacency matrix consumes huge amount of memory for storing big graphs.
• Adjacency list
Allows us to find all nodes adjacent to a given node j efficiently in O(1) time
Adjacent list allows us to store graph in more compact form, than adjacency matrix
In General,
• Adjacency matrix is good for dense graphs.
• Adjacency lists is good for sparse graphs and also for changing the no of
nodes.
Dense Graphs and Sparse Graphs
• Dense graph is a graph in which the number of edges is close
to the maximal number of edges.
• The opposite, a graph with only a few edges, is a sparse
graph.
JFK
LAX STL
HNL
DFW
FTL
Solving a simple problem with Graph
• A network of computers can be represented By a graph, with
each vertex representing One of the machines in the
network, and
• an Edge representing a communication wire Between the two
machines.
v4 v5 4
v2
G
5
v1
Non-recursive BFS example
Start from v5 Visit Queue
(front to back)
v2 v5 v5
v1 v3 empty
v3 v3
v4 v3, v4
v4 v5 v4
v2 v4, v2
G v2
empty
v1 v1
empty
Graphs Traversal
In general:
Both traversal algorithms
1. Start at one vertex of a graph
2. Process the information contained at that vertex
3. Move along an edge to process a neighbor
G Call DFS on G
D
found destination - done!
B
Path is implicitly stored in DFS recursion Path is:
A
A, B, D, G
DFS vs. BFS
BFS Process
F B A start
E
G D C
destination
A B D C D
Initial call to BFS on A Dequeue A Dequeue B Dequeue C
Add B Add C, D Nothing to add
Add A to queue
rear front
G found destination - done!
Dequeue D
Path must be stored separately
Add G
Hamiltonian circuit
• Hamiltonian paths and circuits are named after the Mathematician ,William
Rowan Hamilton.
• A Hamiltonian circuit in a connected graph is defined as a closed walk that
traverses every vertex of G exactly once, also called Hamiltonian cycles.
• It is called as circuit if it includes every vertex of G. If any edge is removed
then it is Hamiltonian path.
Hamiltonian path
Summary
• Graphs are one of the most important non-linear data structures.
• A graph is a representation of relation