0% found this document useful (0 votes)
11 views38 pages

Graphs

The document provides an introduction to graphs as non-linear data structures consisting of vertices and edges, detailing their definitions, types (directed and undirected), and terminologies such as paths, cycles, and degrees. It also discusses applications of graphs in various fields like transportation, computer networks, and databases, and explains two methods for graph representation: adjacency matrix and adjacency list. Additionally, it highlights the concept of bipartite graphs and their complete forms.

Uploaded by

degag64086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Graphs

The document provides an introduction to graphs as non-linear data structures consisting of vertices and edges, detailing their definitions, types (directed and undirected), and terminologies such as paths, cycles, and degrees. It also discusses applications of graphs in various fields like transportation, computer networks, and databases, and explains two methods for graph representation: adjacency matrix and adjacency list. Additionally, it highlights the concept of bipartite graphs and their complete forms.

Uploaded by

degag64086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

CS-250 Data Structures and Algorithms

Graphs
Dr. Muhammad Kamran Khan
Assistant Professor (Comp. Sc)

Department of Electrical Engineering


Military College of Signals
National University of Sciences and Technology (NUST

Data Structures & Algorithms 1


Graph – Introduction
• A Graph is a non-linear data structure consisting of
vertices and edges.

• The vertices are sometimes also referred to as nodes and


the edges are lines or arcs that connect any two nodes in
the graph. More formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The graph is denoted
by G(V, E).

Data Structures & Algorithms 2


Graph – Introduction
• A graph G = (V, E) is composed of:

V: set of vertices

E: set of edges connecting the vertices in V

• An edge e = (u, v) is a pair of vertices

• Example:
V = {a, b, c, d, e}
E = {(a, b), (a, c),
(a, d), (b, e),
(c, d), (c, e),
(d, e)}

Data Structures & Algorithms 3


Graph – Introduction…
• Edges connect two Example: The following picture
is a graph. List its vertices and
vertices. edges.
• Edges only intersect at A
D
vertices.

• Edges joining a vertex C

to itself are called


loops.

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

Data Structures & Algorithms 5


Graph – Introduction…
• Now check out the graph below.
• What can we say about it in comparison to the previous
figure?

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

• One graph may be drawn in (infinitely) many ways, but it


always provides us with the same information.

• Graphs are a structure for describing relationships


between objects.
– The vertices denote the objects and the edges represent the
relationship.

Data Structures & Algorithms 7


Directed vs. Undirected graphs
• When the edges in a graph have no direction, the graph
is called undirected. For example, Graph1

• When the edges in a graph have a direction, the graph is


called directed (or digraph). For example, Graph2

Warning: if the graph is directed, the order


of the vertices in each edge is important !!
Data Structures & Algorithms 8
Trees vs. Graphs
• Trees are special cases of graphs!!

Data Structures & Algorithms 9


Graph – Applications

• 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

Data Structures & Algorithms


Graphs Terminologies…
• Weighted graph:
- A graph is suppose to be weighted if its every edge is
assigned some value which is greater than or equal to zero.

Data Structures & Algorithms 12


Graphs Terminologies
• Adjacent nodes: two nodes are adjacent if they are
connected by an edge
5 adjacent to 7

7 is adjacent from 5

• Complete graph: a graph in which every vertex is directly


connected to every other vertex

Data Structures & Algorithms 13


Graphs Terminologies

• 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

Data Structures & Algorithms


Graphs Terminologies

• 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

Data Structures & Algorithms


Graphs Terminologies

• Connected Graph
– An undirected graph is connected if, for any pair of vertices,
there is a path between them.

Connected Not Connected


– A directed graph is connected if, for any pair of vertices,
there is a path between them.

Data Structures & Algorithms


Graphs Terminologies
• Complete Graph
– A graph in which all pairs of vertices are adjacent OR
– A graph in which every vertex is directly connected to every
other vertex
– Let n = Number of vertices, and
m = Number of edges
– For a complete graph with n vertices, the number of edges
is n(n – 1)/2. A graph with 6 vertices needs 15 edges to be
complete.

n 5
m  (5 

Data Structures & Algorithms


Graphs Terminologies

• 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

Data Structures & Algorithms


Graphs Terminologies
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3G
3 1 1 1 1
0 in:1, out: 1 G2
Directed graph
in-degree
out-degree 1 in: 1, out: 2

2 in: 1, out: 0
G3
Data Structures & Algorithms
Bipartite Graphs
• A simple graph is bipartite if V can be partitioned into
V = V1V2 so that any two adjacent vertices are in
different parts of the partition.

• Another way of expressing the same idea is


bichromatic: vertices can be colored using two colors
so that no two vertices of the same color are adjacent.

Data Structures & Algorithms 21


Bipartite Graphs…
• And so is bipartite, if we redraw it:

Data Structures & Algorithms 22


Complete Bipartite - Km,n
• When all possible edges exist in a simple bipartite graph
with m red vertices and n green vertices, the graph is called
complete bipartite and the notation Km,n is used.

K2,3 K4,5

Data Structures & Algorithms 23


Graph Representation
• There are two methods to represent a Graph
– Adjacency Matrix ( Array Implementation)
– Adjacency List (Linked List Implementation)

Data Structures & Algorithms


Adjacency Matrix
• Let G=(V,E) be a graph with n vertices.
• The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
• If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
• If there is no such edge in E(G), adj_mat[i][j]=0
• The adjacency matrix for an undirected graph is
symmetric; since adj_mat[i][j]=adj_mat[j][i]
• The adjacency matrix for a digraph may not be
symmetric

Data Structures & Algorithms


Adjacency Matrix
• For a digraph G = (V,E ) define matrix AG by:
• Value at i th row and j th column is
– 1 if i th vertex connects to j th vertex (i  j )
– 0 otherwise

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

Data Structures & Algorithms G4


Adjacency Matrix
Q: What is the adjacency matrix?

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

• We can also represent weighted graph with


Adjacency Matrix. Now the contents of the martix will
not be 0 and 1 but the value is substituted with the
corresponding weight.

Data Structures & Algorithms


Another Example
Adjacency Matrix?

1 2

4 5
3

6 7

Data Structures & Algorithms


1 2 3 4 5 6 7
1 0 1 1 1 0 0 0
2 0 0 0 1 1 0 0
3 0 0 0 0 0 1 0
4 0 0 1 0 0 1 1
5 0 0 0 1 0 0 1
6 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0

Data Structures & Algorithms


Adjacency List

• A Single Dimension array of Structure is used to


represent the vertices

• A Linked list is used for each vertex V which


contains the vertices which are adjacent from V
(adjacency list)

Data Structures & Algorithms


0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
Data Structures & Algorithms
Another Example of Adjacency List

Data Structures & Algorithms


Linked-list implementation

Data Structures & Algorithms


Graph Applications
• Modelling a road network with vertices as towns and edge costs as
distances.

• Modelling a water supply network.

• Dynamically modelling the status of a set of routes by which traffic might


be directed over the Internet.

• 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.

Data Structures & Algorithms 73


Summary
• Graph introduction

• Directed vs. Undirected graph

• Few graph terminologies

• Bipartite Graph

• Adjacency Matrix

Data Structures & Algorithms 74


THANK YOU

Data Structures & Algorithms 75

You might also like