Data - Structure - and - Algorithms - Lecture - 9 Graphs
Data - Structure - and - Algorithms - Lecture - 9 Graphs
and Applications
CT-157
Course Instructor
Engr. Adiba Jafar
Data Structure and
Algorithms
Lecture 09
GRAPH
Heaps
A heap is a
certain kind of
complete binary
tree.
Heaps Root
A heap is a
certain kind of
complete binary
tree.
When a complete
binary tree is built,
its first node must be
the root.
Heaps
Complete binary Left child
of the
tree.
root
19
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
Heaps
45
A heap is a
certain kind of
35 23
complete binary
tree.
27 21 22 4
19
The "heap property"
requires that each
node's key is >= the
keys of its children
MAX AND MIN HEAP
Max-Heap − Where the value of the parent node is
greater than or equal to either of its children.
35 21 22 4
19 27
Removing the Top of a Heap
27
❑ Move the last node onto
the root.
42 23
35 21 22 4
19
Removing the Top of a Heap
27
❑ Move the last node onto
the root.
42 23
❑ Push the out-of-place
node downward,
swapping with its larger 35 21 22 4
child until the new node
reaches an acceptable
19
location.
Removing the Top of a Heap
42
❑ Move the last node onto
the root.
27 23
❑ Push the out-of-place
node downward,
swapping with its larger 35 21 22 4
child until the new node
reaches an acceptable
19
location.
Removing the Top of a Heap
42
❑ Move the last node onto
the root.
35 23
❑ Push the out-of-place
node downward,
swapping with its larger 27 21 22 4
child until the new node
reaches an acceptable
19
location.
Removing the Top of a Heap
42
❑ The children all have
keys <= the out-of-place
node, or 35 23
❑ The node reaches the
leaf. 27 21 22 4
❑ The process of pushing
the new node 19
downward is called
reheapification
downward.
Implementing a Heap
42
❑ We will store the
data from the nodes
35 23
in a partially-filled
array.
27 21
An array of data
Implementing a Heap
42
⚫ Data from the root
goes in the first
35 23
location of
the array.
27 21
42
An array of data
Implementing a Heap
42
⚫ Data from the next
row goes in the next
35 23
two array locations.
27 21
42 35 23
An array of data
Implementing a Heap
42
⚫ Data from the next
row goes in the next
35 23
two array locations.
27 21
42 35 23 27 21
An array of data
Implementing a Heap
42
⚫ Data from the next
row goes in the next
35 23
two array locations.
27 21
42 35 23 27 21
An array of data
We don't care what's in
this part of the array.
HUFFMAN’S ALGORITHM
Huffman Coding-
•Huffman Coding is a famous Greedy Algorithm.
•It is used for the lossless compression of data.
•It uses variable length encoding.
•It assigns variable length code to all the characters.
•The code length of a character depends on how frequently it occurs in the
given text.
•The character which occurs most frequently gets the smallest code.
•The character which occurs least frequently gets the largest code.
•It is also known as Huffman Encoding.
Continue
Prefix Rule-
•Huffman Coding implements a rule known as a prefix rule.
•This is to prevent the ambiguities while decoding.
•It ensures that the code assigned to any character is not a
prefix of the code assigned to any other character.
Major Steps in Huffman Coding-
There are two major steps in Huffman Coding-
1.Building a Huffman Tree from the input characters.
2.Assigning code to the characters by traversing the Huffman
Tree.
Huffman’s Tree
The steps involved in the construction of Huffman Tree are
as follows-
Step-01:
•Create a leaf node for each character of the text.
•Leaf node of a character contains the occurring frequency of
that character.
Step-02:
•Arrange all the nodes in increasing order of their frequency
value.
Huffman’s Tree
Step-03:
⚫Considering the first two nodes having minimum frequency,
•Create a new internal node.
•The frequency of this new node is the sum of frequency of those
two nodes.
•Make the first node as a left child and the other node as a right
child of the newly created node.
Step-04:
•Keep repeating Step-02 and Step-03 until all the nodes form a
single tree.
•The tree finally obtained is the desired Huffman Tree.
Important Formulas
EXAMPLE
A file contains the following characters with the frequencies
as shown. If Huffman Coding is used for data compression,
determine-
1.Huffman Code for each character
2.Average code length
3.Length of Huffman encoded message (in bits)
Example
Solution(steps 01 and 02)
First let us construct the Huffman Tree.
Huffman Tree is constructed in the following steps-
Step 03
Step 04
Step 05
Step 05
Step 06
Step 06
Step 07
Step 07
Assigning weights
1. We assign weight to all the edges of the constructed
Huffman Tree.
2. Let us assign weight ‘0’ to the left edges and weight ‘1’ to
the right edges.
Assigning weights
Huffman’s Prefix code
Code length and length of
encoded message
What is a graph?
Graphs represent the relationships among data items
⚫A graph G consists of
⚫ a set V of nodes (vertices)
⚫ a set E of edges: each edge connects two nodes
⚫Each node represents an item
⚫Each edge represents the relationship between two items
node
edge
Examples of graphs
Molecular Structure Computer Network
H Server 1 Terminal 1
H C H
Terminal 2
H Server 2
v
(u, v)
u w
u and v are adjacent
v and w are not adjacent
Path and simple path
⚫ A path from v1 to vk is a sequence of nodes v1, v2, …, vk
that are connected by edges (v1, v2), (v2, v3), …, (vk-1, vk)
⚫ A path is called a simple path if every node appears at
most once.
v2 v3
v1
v2
v1 v3
- v2, v3, v4, v5 , v3, v2 is a cycle
- v2, v3, v4, v2 is a cycle, it is v5
also a simple cycle
v4
Connected graph
⚫ A graph G is connected if there exists path between every
pair of distinct nodes; otherwise, it is disconnected
v2
v1 v3
v4 v5
This is a connected graph because there exists path
between every pair of nodes
Example of disconnected graph
v1 v3 v7 v8
v2
v4 v5
v6 v9
v2 v7 v8
v1 v3
v4 v5
v6 v9
Complete graph
⚫ A graph is complete if each pair of distinct nodes has an
edge
v2 v2
v1 v3 v3
v4 v5 v4 v5
G H
Weighted graph
⚫ If each edge in G is assigned a weight, it is called a
weighted graph
Chicago 1000 New York
3500
2000
Houston
Directed graph (digraph)
⚫ All previous graphs are undirected graph
⚫ If each edge in E has a direction, it is called a directed edge
⚫ A directed graph is a graph where every edges is a directed
edge
Directed edge
2000
3500
Houston
More on directed graph
x y
v2 1 v1 0 1 0 0 0
v1 v3 2 v 0 0 0 1 0
2
3 v3 0 1 0 1 0
v4 v5 4 v 0 0 0 0 0
4
5 v5 0 0 1 1 0
G
Adjacency matrix for weighted
undirected graph
Matrix[i][j] = w(vi, vj) if (vi, vj)∈E or (vj, vi)∈E
∞ otherwise
1 2 3 4 5
v2 v1 v2 v3 v4 v5
v1 2 v3
5 1 v1 ∞ 5 ∞ ∞ ∞
4 3 7 2 v2 5 ∞ 2 4 ∞
8 v5
v4 3 v3 0 2 ∞ 3 7
4 v4 ∞ 4 3 ∞ 8
G
5 v5 ∞ ∞ 7 8 ∞
Adjacency list for directed graph
1 v1 → v2
v2 2 v2 → v4
v1 v3
3 v3 → v2 → v4
4 v4
v4 v5
5 v5 → v3 → v4
G
Adjacency list for weighted undirected
graph
v2
v1 2 v3 1 v1 → v2(5)
5
4 2 v2 → v1(5) → v3(2) → v4(4)
3 7
3 v3 → v2(2) → v4(3) → v5(7)
8 v5
v4 4 v4 → v2(4) → v3(3) → v5(8)
5 v5 → v3(7) → v4(8)
G
Pros and Cons
⚫ Adjacency matrix
⚫ Allows us to determine whether there is an edge from node i
to node j in O(1) time
⚫ Adjacency list
⚫ Allows us to find all nodes adjacent to a given node j
efficiently
Graph Traversal Algorithm
⚫ To traverse a tree, we use tree traversal algorithms
like pre-order, in-order, and post-order to visit all the
nodes in a tree
⚫ Similarly, graph traversal algorithm tries to visit all
the nodes it can reach.
⚫ If a graph is disconnected, a graph traversal that
begins at a node v will visit only a subset of nodes,
that is, the connected component containing v.
Two basic traversal algorithms
⚫ Two basic graph traversal algorithms:
⚫ Depth-first-search (DFS)
⚫ After visit node v, DFS strategy proceeds along a path from v as
deeply into the graph as possible before backing up
⚫ Breadth-first-search (BFS)
⚫ After visit node v, BFS strategy visits every node adjacent to v
before visiting any other nodes
Depth-first search (DFS)
⚫ DFS strategy looks similar to pre-order. From a given node v, it
first visits itself. Then, recursively visit its unvisited neighbours
one by one.
⚫ DFS can be defined recursively as follows.
DFS example
⚫ Start from v3 1 v
3
v2
2 v
v1 v3
x x x 3
2
4
v v
v4
x x v5 1 4
5
v
G
5
Non-recursive DFS example
visit stack
v3 v3
v2 v3, v2 v2
v1 v3
v1
backtrack
v3, v2, v1
v3, v2 x x x
v4
v5
v3, v2, v4
v3, v2, v4 , v5 v4
x x v5
backtrack v3, v2, v4
backtrack v3, v2
backtrack v3 G
backtrack empty
Breadth-first search (BFS)
⚫ BFS strategy looks similar to level-order. From a
given node v, it first visits itself. Then, it visits every
node adjacent to v before visiting any other nodes.
1. Visit v
2. Visit all v’s neigbours
3. Visit all v’s neighbours’ neighbours
…
⚫ Similar to level-order, BFS is based on a queue.
BFS example Visit Queue
(front to
⚫ Start from v5 1 back)
v v5 v5
v2 v3 5 empty
v1 2 v v 3 v3 v3
x x x v4 v3, v4
3 4 v4
vx v
4
4
x v2 v4, v2
v5 2
v2
G empty
5 v v1 v1
empty
1
Floyd Warshall Algorithm-
• Floyd Warshall Algorithm is a famous algorithm.
• It is used to solve All Pairs Shortest Path Problem.
• It computes the shortest path between every pair of
vertices of the given graph.
• Floyd Warshall Algorithm is an example of dynamic
programming approach.
Advantages
⚫ Floyd Warshall’s Algorithm has the following main
advantages-
• It is extremely simple.
• It is easy to implement.
Problem
Solution
Step-01:
Remove all the self loops and parallel edges (keeping the lowest weight edge)
from the graph.
In the given graph, there are neither self edges nor parallel edges.
Step-02:
⚫Write the initial distance matrix.
•It represents the distance between every pair of vertices in the form of given
weights.
•For diagonal elements (representing self-loops), distance value = 0.
•For vertices having a direct edge between them, distance value = weight of that
edge.
•For vertices having no direct edge between them, distance value = ∞.
Step 2
Step 3
Step 4
Continue
HOME TASKS
Problem-01:
Create a min heap for the following data
23,45,1,5,14,25,24,13,11,8,3,19,4
And then apply Heap sort
Problem 02
Apply Floyd Warshall’s Algorithm (use book for alphabetical
path
Home Tasks
Problem 03
Read Prepositions 8.2 and 8.3 and solve these by taking
some graph .