Graphs
Graphs
Graphs
Dr. Muhammad Kamran Khan
Assistant Professor (Comp. Sc)
V: set of vertices
• Example:
V = {a, b, c, d, e}
E = {(a, b), (a, c),
(a, d), (b, e),
(c, d), (c, e),
(d, e)}
B
Data Structures & Algorithms 4
Graph – Introduction…
Example
• This is also a graph. The vertices just happen to have
people’s names (e.g., first character).
• Such a graph could represent friendships (or any kind of
relationship).
F B L
R A P
L R
F
Previous figure
A F B L
B
P Z Z
R A P
Data Structures & Algorithms 6
Graph – Introduction…
Moral of the Story
• Electronic circuits
– Printed circuit board
– Integrated circuit
• Transportation networks
– Highway network
– Flight network
• Computer networks
– Local area network
– Wide area network
– Internet
• Databases
– Entity-relationship
diagram
Data Structures & Algorithms
Graph Applications
• Driving Map
– Edge = Road
– Vertex = Intersection
– Edge weight = Time required to cover the road
• Airline Traffic
– Vertex = Cities serviced by the airline
– Edge = Flight exists between two cities
– Edge weight = Flight time or flight cost or both
• Computer Networks
– Vertex = Server nodes
– Edge = Data link
– Edge weight = Connection speed
7 is adjacent from 5
• Adjacent Nodes
– When there is an edge from one node to another then these
nodes are called adjacent nodes.
• Path
– sequence of vertices v1,v2,. . .vk such that consecutive
vertices vi and vi+1 are adjacent.
a b a b
c c
d ed e
abedc bedc
Data Structures & Algorithms
Graphs Terminologies
• Length of a Path
– Length of a path is nothing but the total number of edges
included in the path from source to destination node.
• Simple Path
– path such that all its vertices and edges are distinct.
a b
bec
c
d e
• Cycle
– simple path, except that the last vertex is the same as the
first vertex
a b
• Acyclic Graph
– A graph without cycle is
called acyclic Graph. A tree c
is a good example of acyclic
graph.
d e
abeca
• Connected Graph
– An undirected graph is connected if, for any pair of vertices,
there is a path between them.
n 5
m (5
• Degree of a node
– In an Undirected graph, the total number of edges linked to
a node is called a degree of that node.
– For directed graph there are two degree for every node
•Indegree
–The indegree of a node is the total number of edges
coming to that node.
•Outdegree
–The outdegree of a node is the total number of edges
going out from that node
2 in: 1, out: 0
G3
Data Structures & Algorithms
Bipartite Graphs
• A simple graph is bipartite if V can be partitioned into
V = V1V2 so that any two adjacent vertices are in
different parts of the partition.
K2,3 K4,5
R digraph(R) MR
2 1 1 1 1
1 1
2 2 0 1 1 1
3
1
3 3
1
0 0 1
4 4 0 1
4 0 0
Data Structures & Algorithms 28
Examples for Adjacency Matrix
0 0 4
0
1 2 2 1 5
3 3 6
1
0 1 1 1 0 1 0
1 0 1 1 7
1 0 1
2 0 1 1 0 0 0 0 0
1 1 0 1 0 0 0 1
0 0 1 0 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2
G
1 0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 0
1 4 3
0 3 0 1
A: 0 1 2 0
0 1 2 0
0 0
0 0
Data Structures & Algorithms 30
Adjacency Matrix
• From the adjacency matrix, to determine the
connection of vertices is easy
n 1
• The degree of a vertex is
adj _ mat[i][ j ]
j 0
1 2
4 5
3
6 7
• Minimising the cost and time taken for air travel when direct flights don't
exist between starting and ending airports.
• Using a directed graph to map the links between pages within a website
and to analyse ease of navigation between different parts of the site.
• Bipartite Graph
• Adjacency Matrix