You are on page 1of 31

UNIT-V

Graphs: Introduction
 Applications of graphs
 Graph representations
 graph traversals
Hashing: Introduction
Hashing Functions-Modulo, Middle of Square, Folding
Collision Techniques-Linear Probing, Quadratic Probing, Double Hashing,
Separate Chaining.
String Algorithms:
 Introduction,
String Matching Algorithm
 Brute Force Method
 Rabin-Karp String Matching Algorithm

S. Durga Devi , CSE, CBIT


Graphs: Introduction
Graph representations
 graph traversals
 Applications of graphs

S. Durga Devi , CSE, CBIT


S. Durga Devi , CSE, CBIT
S. Durga Devi , CSE, CBIT
Applications of graphs
• Graphs applications
1. Data mining: data mining is used to extract information from the huge data.
Mostly data we dealing in data science shaped up with graphs.
2. Google maps and GPS: uses graphs to find the shortest path from one destination
to another.
3. Social networks: facebook, twitter graphs used to represent connection between
users.
4. Operation research: transportation uses graphs to find the optimal path to deliver
goods and services.
5. Chemistry: represents molecules using graph.
6. Flow of execution of a program.

S. Durga Devi , CSE, CBIT


Introduction to Graphs
 Another non linear data structure.
 In tree structure, there is a hierarchical relationship between
parent and children, that is, one parent and many children.
 On the other hand, in graph, the relationship is from many
parents to many children.
Definition-A graph G = (V,E) is composed of:
V: set of vertices(or nodes)
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example: a b
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),(b,e),
c (c,d),(c,e),(d,e)}

d e
S. Durga Devi , CSE, CBIT
Graph terminology
• Digraph- A digraph is also called a directed graph. It is a graph G, such
that, G=<V,E>, where V is the set of all vertices and E is the set of ordered
pair of elements from V.
• Edges have directions
v= A,B,C,D
E=((A,B),(A,C),(A,D),(D,B))
A B A B These are directed graphs

C C
D D

G directed

 
if edges ordered pairs (u,v)
u v
G undirected-

 
if edges unordered pairs {u,v}
S. Durga Devi , CSE, CBIT
Adjacent vertices
If two nodes are connected by an edge, they are neighbors (and the nodes are
adjacent to each other).
 The size of a graph is the number of nodes in it
 The empty graph has size zero (no nodes).
Loop- An edge that connects the vertex with itself
Degree of a Vertex- the number of edges connected to that vertex
• For directed graph,
– the in-degree of a vertex v is the number of edges incident on to the vertex.
– the out-degree of a vertex v is the number of edges emanating from that
vertex. 3 0
Examples
0 1 2
2
3 3
3 1 2 3 3 4 5 6
3
1 1 1 1
3 0 in:1, out: 1
directed graph
in-degree 1 in: 1, out: 2
S. Durga Devi , CSE, CBIT out-degree
2 in: 1, out: 0
 path: sequence of vertices v1,v2,. . .vk such that consecutive
vertices vi and vi+1 are adjacent.
a b a b

c
c
d e d e a b
abedc
simple path: no vertex is repeated c
bec

Cycle - some path with distinct vertices except that the last d e
vertex is the same as the first vertex

Parallel edges- if there are a b


more than one edge between
acda
the same pair of vertices. c

S. Durga Devi , CSE, CBIT d e


 A graph without cycles is called acyclic graph.

 Sub graph is a graph with subset of vertices and edges of a graph.

 A graph is called a simple graph if it has no loops and no parallel edges.

 A graph is termed as weighted graph if all the edges in it are labeled with
some weights.
 A complete graph is a graph if every node in a graph is connected to
every other node.
 Two vertices are said to be connected if there is a path from vi to vj or vj to
vi
 connected graph: any two vertices are connected by some path. If there is a
path from vi to vj or vj to vi is called connected graph

 A directed graph is said to be strongly connected if for every pair of distinct


vertices vi,vj in G, there is a path from vi to vj and also from vj to vi.

 Weakly connected graph- if a graph is not strongly connected but it is


connected S. Durga Devi , CSE, CBIT
Cyclic graph Acyclic graph Weighted Graph
A A

B C B C

D D

Complete Graph Strongly Connected

A B A

D B
C D
C
E
0 0 0 1 2 0
1 2 1 2 3 1 2
3 3
G1
(i) ii) (iii) (iv)

(a) Some of the sub graph of G1

0
0 0 0
1 1 1
2 2
G2 (i) (ii) (iii)

(b) Some of the sub graph of G2


More definitions : Loop

An edge that connects the vertex with itself


Or an edge whose starting and end vertices are same

A B

C
D
Even More Terminology
•connected graph: any two vertices are connected by some path

At least two vertices


are not connected in a
graph is called
disconnected graph.

connected not connected


Subgraphs Examples
0
0 0 1 2 0
1 2
1 2 3 1 2
3
G1 (i) (ii) (iii) 3(iv)
(a) Some of the subgraph of G1
0
0 0 0 0

1 1 1 1

2 2 2
(i) (ii) (iii) (iv)
G3 (b) Some of the subgraph of G3
Simple graph
• Simple graph- A graph (digraph) if it does
not have any loops or parallel edges (edge
from v1 to v2 and v2 to v1) is called
simple graph.
Representation of a Graph

There are two ways of representing a graph in memory:

Sequential Representation by means of Adjacency Matrix.

Linked Representation by means of Linked List.


 Adjacency Matrix is a bit matrix which contains entries of only 0 and 1
 Since it stores the information of whether the adjacency vertices are present
or not.
 The connected edge between two vertices is represented by 1 and absence
of edge is represented by 0.

 This representation uses a square matrix of order n x n, where n is the


number of vertices in the graph.

 Adjacency matrix of an undirected graph is symmetric.


Adjacency Matrix
A
A B C D E
A 0 1 1 1 0
B C B 1 0 1 1 0
C 1 1 0 1 1
D E D 1 1 1 0 1
E 0 0 1 1 0
Linked List Representation

 It saves the memory.


 The number of lists depends on the number of vertices in the graph.
Each vertex in graph, maintains the list of its neibours.

 The header node in each list maintains a list of all adjacent vertices of a
node .
A B C D NULL

D NULL
A
B A C

D E NULL B C
C A B

D A B C E NULL D E

N E C D NULL
Undirected Graph Adjacency List Adjacency Matrix
Directed Graph Adjacency List Adjacency Matrix
Comparison among various representations

• Adjacency matrix representation always requires n x n matrix


with n vertices, regardless of the number of edges. But from
the manipulation point of view, this representation is the best.
• Insertion, deletion and searching are concerned, in some cases,
linked list representation has an advantage, but overall
performance is consider, matrix representation of graph is
more powerful than all
Graph Traversal Techniques
There are two standard graph traversal techniques:
Depth-First Search (DFS)
Breadth-First Search (BFS)

 Traversing a graph means visiting all the vertices in the graph exactly
once.
 DFS and BFS traversals result an acyclic graph.
 DFS and BFS traversal on the same graph do not give the same order of
visit of vertices.
 The order of visiting nodes depends on the node we have selected first
and hence the order of visiting nodes may not be unique.
Depth-First Search
 DFS is similar to the in order traversal of a tree
 Stack is used to implement the Depth- First- Search.
 DFS follows the following rules:
1. Select an unvisited node X, visit it, and treat as the current
node
2. Find an unvisited adjacent of the current node as Y, visit it,
and make it the new current node;
3. Repeat step 1 and 2 till there is a ‘dead end’. dead end means
a vertex which do not have adjacent nodes or no more nodes
can be visited.
4.after coming to a dead end we backtracking along to its parent
to see if it has another adjacent vertex other than Y and then
repeat the steps 1,2,3. until all vertices are visited.
Depth-First Search

 A stack can be used to maintain the track of all paths from any
vertex so as to help backtracking.
 Initially starting vertex will be pushed onto the stack.
 To visit a vertex, we are to pop a vertex from the stack, and
then push all the adjacent vertices onto it.
 A list is maintained to store the vertices already visited.
 When a vertex is popped, whether it is already visited or not
that can be known by searching the list.
 If the vertex is already visited, we will simply ignore it and we
will pop the stack for the next vertex to be visited.
 This procedure will continue till the stack not empty
Breadth First Traversal:
The breadth first traversal is similar to the pre-order traversal of a binary tree.

The breadth first traversal of a graph is similar to traversing a binary tree


level by level (the nodes at each level are visited from left to right).

All the nodes at any level, i, are visited before visiting the nodes at level i + 1.

To implement the breadth first search algorithm, we use a queue.


BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root in a BFS tree being
formed. Its level is called the current level.
2. From each node x in the current level, visit all the unvisited neighbors of
x. The newly visited nodes from this level form a new level that becomes
the next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
A
Graph

B C D

B C D
A

E
B C D
The Depth First Search Tree Order : A, B, E, D, C

The Breadth First Search Tree Order : A, B, C, D, E


A B C A B C

D E F D E F

G H I G H I
DFS Traversal Order BFS Traversal Order

A B C F E G D H I A B D E C G F H I
Practice problems
v1

Dfs-
v2 v8 v3
Bfs-

v4

v5 v6

v7
Applications of Graphs
 Electronic circuits
 Printed circuit board
 Integrated circuit
 Transportation networks
 Highway network
 Flight network
 Computer networks
 Local area network
 Internet
 Web

 Databases
 Entity-relationship diagram

You might also like