1. Unit - 5
Graph and Hashing
Prepared By:
Ms. Purvi Tandel, Chandni Naik
2. Topics of Unit 5
• Graph-Definitions and Concepts
• Matrix and list representation of graphs
• Elementary graph operations - Breadth first search
• Elementary graph operations - Depth first search
• Hashing - Hashing functions
• Collision-Resolution techniques.
3. Introduction of Graph
• Graph is a non-linear data structure.
• It contains a set of points known as nodes (or vertices) and a set of links
known as edges (or Arcs).
• Here edges are used to connect the vertices.
• Generally, a graph G is represented as G = ( V , E ), where V is set of
vertices and E is set of edges.
Example:
• Graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph is a collection of nodes and edges in which nodes are connected with edges.
4. Graph Definitions
1) Graph: A graph G is defined as an ordered set (V, E), where V(G) represents
the set of vertices and E(G) represents the edges that connect these
vertices.
2) Vertex: Individual data element of a graph is called as Vertex. Vertex is also
known as node.
3) Edge: An edge is a connecting link between two vertices. Edge is also known
as Arc.
5. Graph Definitions
4) Adjacent Node: For every edge, e =
(u, v) that connects nodes u and v, the
nodes u and v are the end-points and
are said to be the adjacent nodes or
neighbors.
In example A and B are adjacent
nodes.
5) Directed graph: In a directed
graph, edges form an ordered pair. If
there is an edge from A to B, then
there is a path from A to B but not
from B to A.
The edge (A, B) is said to initiate
from node A (also known as initial
node) and terminate at node B
(terminal node).
6. Graph Definitions
6) Undirected Graph: In an
undirected graph, edges do not have
any direction associated with them.
That is, if an edge is drawn between
nodes A and B, then the nodes can be
traversed from A to B as well as from
B to A.
7) Loop: An edge that has identical
end-points is called a loop.
That is, e = (u, u).
7. Graph Definitions
8) Parallel Edge: In some directed as
well as undirected graphs, we may
have certain pairs of nodes joined by
more than one edges, such edges are
called Parallel edges.
In example edges between node 1
and 2 are parallel edges.
9) Distinct Edge: In case of directed
edges, two possible edges between
any pair of nodes which are opposite
in direction are considered Distinct.
In example edges between node 2
and 3 are distinct edges.
2
1
4
3
2
2
1
1
1
2
2
1
4
3
2
2
1
1
1
8. Graph Definitions
10) Weighted Graph: In a weighted
graph, the edges of the graph are
assigned some weight or length. The
weight of an edge denoted by w(e) is
a positive value which indicates the
cost of traversing the edge.
11) Isolated Graph: In a graph a
node which is not adjacent to any
other node is called isolated node.
9. Graph Definitions
12) Null Graph: A graph having no edges is called a Null Graph.
A graph containing only isolated nodes are called null graph.
13) Degree of node: The degree of a node, written as deg(u), is equal to the
sum of in-degree and out-degree of that node.
degree(u) = indegree(u) + outdegree(u)
Indegree : The no of edges which have V as their terminal node is call as indegree of node V.
Outdegree : The no of edges which have V as their initial node is call as outdegree of node V.
Degree of A = 2, Degree of B = 3
Degree of C = 2, Degree of D = 3
Degree of E = 2
10. Matrix and list representation of graphs
Graph data structure is represented using following representations.
• Adjacency Matrix
• Adjacency List (Linked List)
11. Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: In this representation, the graph is represented using a matrix of
size total number of vertices by a total number of vertices. That means a graph with 4
vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns
represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is
an edge from row vertex to column vertex and 0 represents that there is no edge from
row vertex to column vertex.
Undirected
graph
representation
12. Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: In this representation, the graph is represented using a matrix of
size total number of vertices by a total number of vertices. That means a graph with 4
vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns
represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is
an edge from row vertex to column vertex and 0 represents that there is no edge from
row vertex to column vertex.
Directed graph
representation
13. Matrix and list representation of graphs
Graph data structure is represented using following representations.
1) Adjacency Matrix: One more example in directed graph representation.
For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the
elements of V.
V1
V2 V3
V4
0 1 0 1
1 0 0 0
1 1 0 1
0 1 0 0
V1
V1
V2
V2
V3
V3
V4
V4
A =
14. Matrix and list representation of graphs
Graph data structure is represented using following representations.
2) Adjacency List: In this representation, every vertex of graph contains list of its adjacent
vertices.
For example, consider the following directed graph representation implemented using
linked list.
15. Matrix and list representation of graphs
Graph data structure is represented using following representations.
2) Adjacency List: One more example of undirected graph representation.
0
1
3
2
4
0
1
2
3
4
1 2 3 4
0 3
0 3 4
0 1 2 4
0 2 3
16. Elementary graph operations – DFS & BFS
• One of the elementary operation on graph is graph traversal.
• Graph traversal is technique used for searching a vertex in a graph.
• The graph traversal is also used to decide the order of vertices to be visit in the
search process.
• A graph traversal finds the edges to be used in the search process without
creating loops that means using graph traversal we visit all vertices of graph
without getting into looping path.
There are two graph traversal techniques:
• DFS (Depth First Search)
• BFS (Breadth First Search)
17. DFS – Depth First Search
Depth First Search (DFS) algorithm traverses a graph in a depth-ward motion and
uses a stack to remember to get the next vertex to start a search, when a dead
end occurs in any iteration.
A
B C
D E F G
H
A B D H E C F G
✓
✓
✓
✓
✓
✓
✓ ✓
18. DFS – Depth First Search
We use the following steps to implement DFS traversal.
1) Define a Stack of size total number of vertices in the graph.
2) Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
3) Visit any one of the adjacent vertex of the vertex which is at top of the stack
which is not visited and push it on to the stack.
4) Repeat step 3 until there are no new vertex to be visit from the vertex on top
of the stack.
5) When there is no new vertex to be visit then use back tracking and pop one
vertex from the stack.
6) Repeat steps 3, 4 and 5 until stack becomes Empty.
7) When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph.
19. DFS – Depth First Search – Example 1
1 Initialize the stack.
2
Mark S as visited and put it onto the stack. Explore any
unvisited adjacent node from S. We have three nodes and
we can pick any of them. For this example, we shall take
the node in an alphabetical order.
3
Mark A as visited and put it onto the stack. Explore any
unvisited adjacent node from A. Both S and D are adjacent
to A but we are concerned for unvisited nodes only.
20. DFS – Depth First Search – Example 1
4 Visit D and mark it as visited and put onto the
stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However,
we shall again choose in an alphabetical order.
5
We choose B, mark it as visited and put onto the stack.
Here B does not have any unvisited adjacent node. So, we
pop B from the stack.
6
We check the stack top for return to the previous
node and check if it has any unvisited nodes. Here,
we find D to be on the top of the stack.
21. DFS – Depth First Search – Example 1
7
Only unvisited adjacent node is from D is C now.
So we visit C, mark it as visited and put it onto
the stack.
For DFS sequence, write the vertex in order of
their visited sequence.
DFS sequence (visited sequence): S A D B C
Resulted spanning tree
Spanning tree:
A spanning tree of a connected, undirected graph G
is a sub-graph of G which is a tree that connects all
the vertices together.
Answer:
22. Introduction to Minimum spanning tree
Spanning Tree:
• A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that
connects all the vertices together.
• A graph G can have many different spanning trees. We can assign weights to each edge
(which is a number that represents how unfavorable the edge is), and use it to assign a
weight to a spanning tree by calculating the sum of the weights of the edges in that
spanning tree.
23. Introduction to Minimum spanning tree
Minimum spanning tree:
• A minimum spanning tree (MST) is defined as a spanning tree with weight less than or
equal to the weight of every other spanning tree.
• In other words, a minimum spanning tree is a spanning tree that has weights associated
with its edges, and the total weight of the tree (the sum of the weights of its edges) is at a
minimum.
24. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A
Answer:
25. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C
Answer:
26. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D
Answer:
27. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F
Answer:
28. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
29. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
30. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
31. DFS – Depth First Search – Example 2
DFS sequence
(visited sequence):
A B C E D F G
Answer:
32. DFS – Depth First Search – Exercise Question
Starting Vertex A:
ABEFCGIHD
Starting Vertex H:
HEFCBGI (A and D are
unreachable)
Answer:
Find the sequence of traverse using DFS.
a. Starting point is A
b. Starting point is H
33. BFS – Breadth First Search
Breadth First Search (BFS) algorithm traverses a graph in a breadth-ward motion
and uses a queue to remember to get the next vertex to start a search, when a
dead end occurs in any iteration.
A
B C
D E F G
H
A| B C| D E F G| H
✓ ✓
✓ ✓ ✓
✓
✓
✓
34. We use the following steps to implement BFS traversal.
1) Define a Queue of size total number of vertices in the graph.
2) Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
3) Visit all the adjacent vertices of the vertex which is at front of the Queue
which is not visited and insert them into the Queue.
4) When there is no new vertex to be visit from the vertex at front of the Queue
then delete that vertex from the Queue.
5) Repeat step 3 and 4 until queue becomes empty.
6) When queue becomes Empty, then produce final spanning tree by removing
unused edges from the graph.
BFS – Breadth First Search
35. BFS – Breadth First Search – Example 1
1 Initialize the queue.
2
We start from visiting S (starting node), and mark
it as visited.
3
We then see an unvisited adjacent node from S.
In this example, we have three nodes but
alphabetically we choose A, mark it as visited
and enqueue it.
36. BFS – Breadth First Search – Example 1
4 Next, the unvisited adjacent node
from S is B. We mark it as visited and
enqueue it.
5
Next, the unvisited adjacent node from S is C.
We mark it as visited and enqueue it.
6
Now, S is left with no unvisited adjacent
nodes. So, we dequeue and find A.
37. BFS – Breadth First Search – Example 1
7
From A we have D as unvisited adjacent
node. We mark it as visited and
enqueue it.
For BFS sequence, write the vertex in
order of their visited sequence.
BFS sequence (visited sequence): S A B C D
Resulted spanning tree
Spanning tree:
A spanning tree of a connected, undirected graph G
is a sub-graph of G which is a tree that connects all
the vertices together.
Answer:
38. BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A
Answer:
39. BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B
Answer:
40. BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F
Answer:
41. BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F G
Answer:
42. BFS – Breadth First Search – Example 2
BFS sequence
(visited sequence):
A D E B C F G
Answer:
43. BFS – Breadth First Search – Exercise Question
Starting Vertex A:
ABCDEGFIH
Starting Vertex H:
HEICFBG (A and D are
unreachable)
Answer:
Find the sequence of traverse using BFS.
a. Starting point is A
b. Starting point is H
44. Differentiate DFS and BFS
DFS
• DFS stands for Depth First Search.
• DFS starts the traversal from the
root node and explore the search as
far as possible from the root node.
i.e. depthwise
• DFS can be done using stack (LIFO
implementation)
BFS
• BFS stands for Breadth First Search.
• BFS starts traversal from the root
node and then explore the search
level by level. i.e. as close as
possible from root node
• BFS can be done using queue (FIFO
implementation)
46. Minimum Spanning Tree (MST)
• Let 𝐺 = 𝑁, 𝐴 be a connected, undirected graph where,
1. 𝑁 is the set of nodes and
2. 𝐴 is the set of edges.
• Each edge has a given positive length or weight.
• A spanning tree of a graph 𝐺 is a sub-graph which is basically a tree and it
contains all the vertices of 𝐺 but does not contain cycle.
• A minimum spanning tree (MST) of a weighted connected graph 𝐺 is a
spanning tree with minimum or smallest weight of edges.
• Two Algorithms for constructing minimum spanning tree are,
1. Kruskal’s Algorithm
2. Prim’s Algorithm
47. Minimum Spanning Tree (MST) – Prim’s algorithm
A
B
C D
E
4
2
1
7
5
3
6 6
5
A – B | 4
A – E | 5
A – C | 6
A – D | 6
B – E | 3
B – C | 2
C – E | 5
C – D | 1
D – E | 7
Let X be the set of nodes
explored, initially X = { A }
A
Step 1: Taking minimum Weight
edge of all Adjacent edges of X={A}
A
B
4
X = { A , B }
Step 2: Taking minimum weight
edge of all Adjacent edges of
X = { A , B }
A
B
4
2
C
X = {A ,B,C}
Step 3: Taking minimum
weight edge of all Adjacent
edges of X = { A , B , C }
A
B
4
2
C D
1
X = {A,B,C,D}
A
B
4
2
C D
1
X = {A,B,C,D,E}
Step 4: Taking minimum
weight edge of all Adjacent
edges of X = {A ,B ,C ,D }
E
3
We obtained minimum
spanning tree of cost:
4 + 2 + 1 + 3 = 10
48. Minimum Spanning Tree (MST) – Kruskal’s algorithm
A
B
C D
E
4
2 7
5
3
6 6
5
Step 1: Taking min
edge (C,D)
1
C D
1
Step 2: Taking next
min edge (B,C)
B
C D
2
1
Step 3: Taking next
min edge (B,E)
B
C D
E
2
3
1
Step 4: Taking next
min edge (A,B)
B
C D
E
2
3
1
A
4
so we obtained minimum
spanning tree of cost:
4 + 2 + 1 + 3 = 10
50. Shortest Path Algorithm – Dijkstra’s algorithm
• Let G = (V,E) be a simple diagraph with n vertices
• The problem is to find out shortest distance from a vertex to all other
vertices of a graph
• Dijkstra Algorithm – it is also called Single Source Shortest Path Algorithm
51. Shortest Path Algorithm – Dijkstra’s algorithm
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A B C D E F
0 ∞ ∞ ∞ ∞ ∞
0 0 0 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4
5
7
6
2
A B C D E F
0 ∞ ∞ ∞
0 0 0
Distance
Visited
0
1st Iteration: Select Vertex A with minimum distance
0
∞
1
1 0 0
∞
5
1 5
∞
∞
∞
Example
52. Shortest Path Algorithm – Dijkstra’s algorithm Example
A
B
C
D
E
F
1
1
5
2
4 7
6
2
0
2nd Iteration: Select Vertex B with minimum distance
1
2
Cost of going to C via B = dist[B] + cost[B][C] = 1 + 1 = 2
Cost of going to D via B = dist[B] + cost[B][D] = 1 + 2 = 3
Cost of going to E via B = dist[B] + cost[B][E] = 1 + 4 = 5
Cost of going to F via B = dist[B] + cost[B][F] = 1 + ∞ = ∞
A B C D E F
0 1 5 ∞ ∞ ∞
1 0 0 0 0 0
Distance
Visited
3
5
∞
A B C D E F
0 1 2 3 5 ∞
0 0 0
Distance
Visited 1 1 0
5
53. Shortest Path Algorithm – Dijkstra’s algorithm Example
3rd Iteration: Select Vertex C via B with minimum distance
A B C D E F
0 1 2 3 5 ∞
1 1 0 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
0
1 3
2 5
∞
Cost of going to D via C = dist[C] + cost[C][D] = 2 + ∞ = ∞
Cost of going to E via C = dist[C] + cost[C][E] = 2 + 5 = 7
Cost of going to F via C = dist[C] + cost[C][F] = 2 + ∞ = ∞
A B C D E F
0 1 2 3 5 ∞
0 0 0
Distance
Visited 1 1 1
5
54. Shortest Path Algorithm – Dijkstra’s algorithm Example
4th Iteration: Select Vertex D via path A - B with minimum distance
A B C D E F
0 1 2 3 5 ∞
1 1 1 0 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
1 3
2 5
9
Cost of going to E via D = dist[D] + cost[D][E] = 3 + 7 = 10
Cost of going to F via D = dist[D] + cost[D][F] = 3 + 6 = 9
A B C D E F
0 1 2 3 5 9
1 0 0
Distance
Visited 1 1 1
5
55. Shortest Path Algorithm – Dijkstra’s algorithm Example
4th Iteration: Select Vertex E via path A – B – E with minimum distance
A B C D E F
0 1 2 3 5 9
1 1 1 1 0 0
Distance
Visited
A
B
C
D
E
F
1
1
5
2
4 7
6
2
1 3
2 5
7
Cost of going to F via E = dist[E] + cost[E][F] = 5 + 2 = 7
A B C D E F
0 1 2 3 5 7
1 1 0
Distance
Visited 1 1 1
Shortest Path from A to F is
A B E F = 7
56. Shortest Path Algorithm – Dijkstra’s algorithm Exercise
Find out shortest path from node 0 to all other nodes using Dijkstra Algorithm
0
1
2 3
4
10
100
30
60
50 10
20
0
1
2 3
4
10
100
30
60
50 10
20
0
10
50
60
30
You can safely remove this slide. This slide design was provided by SlideModel.com – You can download more templates, shapes and elements for PowerPoint from http://slidemodel.com