UNIT-5 1 (Graphs)
UNIT-5 1 (Graphs)
UNIT-5 1 (Graphs)
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
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
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
B C B C
D D
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)
0
0 0 0
1 1 1
2 2
G2 (i) (ii) (iii)
A B
C
D
Even More Terminology
•connected graph: any two vertices are connected by some path
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
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
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.
All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
B C D
B C D
A
E
B C D
The Depth First Search Tree Order : A, B, E, D, 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