0% found this document useful (0 votes)
8 views128 pages

Tree Graph

types of tree, binary tree, complete tree, directed,undirected,prims algorithm ,kruskal algorithm

Uploaded by

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

Tree Graph

types of tree, binary tree, complete tree, directed,undirected,prims algorithm ,kruskal algorithm

Uploaded by

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

CS201: Data Structures and

Discrete Mathematics I
Introduction to trees and graphs

07/14/2024 CS201 1
Trees

07/14/2024 CS201 2
What is a tree?
• Trees are structures used to represent
hierarchical relationship
• Each tree consists of nodes and edges
• Each node represents an object
• Each edge represents the relationship between
two nodes.

nod
e edg
e

07/14/2024 CS201 3
Some applications of Trees
Organization Expression
Chart Tree
Preside
nt +
VP VP
Personn Marketin * 5
el g

3 2
Director Direct
Custom or
er Sales
Relation

07/14/2024 CS201 4
Terminology I
• For any two nodes u and v, if there is an edge
pointing from u to v, u is called the parent of v
while v is called the child of u. Such edge is
denoted as (u, v).
• In a tree, there is exactly one node without
parent, which is called the root. The nodes
without children are called leaves.
roo
t
u
u: parent of
v
v: child of u v
07/14/2024 CS201 leave
5
Terminology II
• In a tree, the nodes without children are
called leaves. Otherwise, they are called
internal nodes.

internal
nodes

leave
s
07/14/2024 CS201 6
Terminology III
• If two nodes have the same parent, they are
siblings.
• A node u is an ancestor of v if u is parent of v or
parent of parent of v or …
• A node v is a descendent of u if v is child of v or
child of child of v or …
u

v and w are siblings


u and v are ancestors of v w
x
v and x are descendents x
of u
07/14/2024 CS201 7
Terminology IV
• A subtree is any node together with all its
descendants.

T
A subtree of
T
v v

07/14/2024 CS201 8
Depth: The depth of a node is the number of
edges from the root to that node.
•Tree Depth: The maximum depth among all
nodes.

• A ← depth 0 Depth of tree = 3


• /\
• B C ← depth 1
• /\
• D E ← depth 2
• \
• F ← depth 3
Height of a Tree
• Definition: The height of a node is the number of edges
on the longest path from that node to a leaf.
• Tree Height: The height of the root node.
• Measured from node to furthest leaf.
• A ← height 3
• /\
• B C ← height 0 for B, height 2 for C
• /\
• D E ← height 0 for D, height 1 for E
• \
• F ← height 0 Height of tree =
3
Terminology
• Level:The level of a node is defined as its depth + 1.
• Degree of a Node:
• The degree of a node is the number of children that
node has.
Binary Tree
• Binary Tree: Tree in which every node has at
most 2 children
• Left child of u: the child on the left of u
• Right child of u: the child on the right of u
S
x: left child of
u v u
y: right child of
x y u
w
w: right child of
z v
07/14/2024 CS201 z: left child12of
Property of binary tree (I)
• A full binary tree of height h has 2h-1
nodes
No. of nodes = 20 + 21 + … + 2(h-1)
= 2h+1 – 1
Level 1: 20
nodes

Level 2: 21
nodes
Level 3: 22
nodes
07/14/2024 CS201 13
Property of binary tree (II)
• Consider a binary tree T of height h. The
number of nodes of T ≤ 2h-1

Reason: you cannot have more nodes than


a full binary tree of height h.

07/14/2024 CS201 14
Property of binary tree (III)
• The minimum height of a binary tree with n
nodes is log(n+1)

By property (II), n ≤ 2h-1


Thus, 2h ≥ n+1
That is, h ≥ log2 (n+1)

07/14/2024 CS201 15
Types of Binary Tree

On the basis of the completion of levels:

. Full Binary Tree


• Complete Binary Tree
• Perfect Binary Tree
• Degenerate (or pathological) tree
Full Binary Tree
A Binary Tree is a full binary tree if every
node has 0 or 2 children.
Complete Binary Tree
Binary Tree is a Complete Binary Tree if all the levels are
completely filled except possibly the last level and the last
level has all keys as left as possible.
The last leaf element might not have a right sibling i.e. a
complete binary tree doesn’t have to be a full binary tree.
A
/ \
B C
/ \ /
D E F
Perfect Binary Tree
A Binary tree is a Perfect Binary Tree in which all the
internal nodes have two children
All leaf nodes are at the same level.
Degenerate tree

A Tree where every internal node has one child


Representation of a Binary Tree
• An array-based representation
• A reference-based representation

07/14/2024 CS201 21
An array-based representation
Array Representation is another way to represent binary
trees, especially useful when the tree is complete (all
levels are fully filled except possibly the last, which is
filled from left to right).

D For any node at index i:


Left Child: Located at 2 * i + 1
Right Child: Located at 2 * i + 2
B F

A C E D B F A C E

0 1 2 3 4 5

22
Reference Based
Representation
NULL: empty tree left element righ
t
You can code this with a
class of three fields:
Object element;
BinaryNode left;
BinaryNode right;
d d

b f
b f
a c
a c
07/14/2024 CS201 23
Balanced Binary Tree
It is a type of binary tree in which the
difference between the height of the left and
the right subtree for each node is either 0 or
1. In the following above, the root node
having a value 0 is unbalanced with a depth
of 2 units.
Tree Traversal
• Given a binary tree, we may like to do
some operations on all nodes in a binary
tree. For example, we may want to double
the value in every node in a binary tree.
• To do this, we need a traversal algorithm
which visits every node in the binary tree.

07/14/2024 CS201 27
Ways to traverse a tree
• There are three main ways to traverse a tree:
– Pre-order:
• (1) visit node, (2) recursively visit left subtree, (3) recursively
visit right subtree
– In-order:
• (1) recursively visit left subtree, (2) visit node, (3) recursively
right subtree
– Post-order:
• (1) recursively visit left subtree, (2) recursively visit right
subtree, (3) visit node
– Level-order:
• Traverse the nodes level by level
• In different situations, we use different traversal
algorithm.

07/14/2024 CS201 28
Examples for expression tree
• By pre-order, (prefix)
+*23/84
• By in-order, (infix)
+
2*3+8/4
• By post-order, (postfix) * /
23*84/+
2 3 8 4
• By level-order,
+*/2384
• Note 1: Infix is what we read!
• Note 2: Postfix expression can be computed
efficiently using stack

07/14/2024 CS201 29
Pre-order
Algorithm pre-order(BTree x)
If (x is not empty) {
print x.getItem(); // you can do other things!
pre-order(x.getLeftChild());
pre-order(x.getRightChild());
}

07/14/2024 CS201 30
Pre-order example

Pre- Print a; Print b; Print d;


order(a); Pre- Pre-order(d); Pre-
order(b); Pre- order(null);
Pre- order(null); Pre-
order(c); order(null);
Print c;
Pre-
order(null);
Pre-
order(null);
a

a b d c b c

d
07/14/2024 CS201 31
Time complexity of Pre-order
Traversal
• For every node x, we will call
pre-order(x) one time, which performs
O(1) operations.
• Thus, the total time = O(n).

07/14/2024 CS201 32
In-order and post-order
Algorithm in-order(BTree x)
If (x is not empty) {
in-order(x.getLeftChild());
print x.getItem(); // you can do other things!
in-order(x.getRightChild());
}

Algorithm post-order(BTree x)
If (x is not empty) {
post-order(x.getLeftChild());
post-order(x.getRightChild());
print x.getItem(); // you can do other things!
}

07/14/2024 CS201 33
In-order example

In- In- In-order(d); In-


order(a); order(b); Print b; order(null);
Print a; In- Print d;
In- order(null); In-
order(c); order(null);
In-
order(null);
Print c;
In-
order(null);
a

d b a c b c

07/14/2024 CS201 34
Post-order example

Post- Post- Post-order(d); Post-


order(a); order(b); Post- order(null);
Post- order(null); Post-
order(c); Print b; order(null);
Print a; Print d;
Post-
order(null);
Print c;
Post-
order(null);
a

d b c a b c

d
07/14/2024 CS201 35
Time complexity for in-order and
post-order
• Similar to pre-order traversal, the time
complexity is O(n).

07/14/2024 CS201 36
General tree implementation
struct TreeNode A
{
Object element
TreeNode *firstChild B C D E
TreeNode *nextsibling
}
F G
because we do not know how many children a
node has in advance.

• Traversing a general tree is similar to traversing


a binary tree

07/14/2024 CS201 37
Binary search tree
A Binary Search Tree (or BST) is a data structure
used in computer science for organizing and storing
data in a sorted manner.
Each node in a Binary Search Tree has at most
two children, a left child and a right child, with
the left child containing values less than the parent
node and the right child containing values greater
than the parent node.
This hierarchical structure allows for efficient
searching, insertion and deletion operations on
the data stored in the tree.
BST
Traversing
Inorder Traversal: 10 20 30 100
150 200 300 (sorted)
Preorder Traversal: 100 20 10
30 200 150 300
Postorder Traversal: 10 30 20
150 300 200 100
Search Operation
The algorithm depends on the property of BST that
if each left subtree has values below root and each
right subtree has values above the root.
If the value is below the root, we can say for sure
that the value is not in the right subtree;
we need to only search in the left subtree and if the
value is above the root, we can say for sure that the
value is not in the left subtree; we need to only
search in the right subtree.
Search
• Find value:4
construct a Binary Tree
Step 1:Insert 40
•Tree is empty. So 40 becomes the root.
•40
Step 2. Insert 20
•20 < 40 → insert to the left of 40.
40
• /
•20
Step 3. Insert 60
•60 > 40 → insert to the right of 40.
• 40
• / \
• 20 60
Creation
Step 4. Insert 10
•10 < 40 → go left
•10 < 20 → insert to the left of 20
• 40
• / \
• 20 60
• /
• 10
Creation
Insert 30
•30 < 40 → go left
•30 > 20 → insert to the right of 20.
• 40
• / \
• 20 60
• / \
• 10 30
Insertion
Given a BST, the task is to insert a new node
in this BST.
How to Insert
A new key is always inserted at the leaf by
maintaining the property of the binary search tree.
We start searching for a key from the root until we
hit a leaf node. Once a leaf node is found, the new
node is added as a child of the leaf node.
• Initilize the current node (say, currNode or node) with
root node
• Compare the key with the current node.
• Move left if the key is less than or equal to the current
node value.
• Move right if the key is greater than current node value.
• Repeat steps 2 and 3 until you reach a leaf node.
• Attach the new key as a left or right child based on the
comparison with the leaf node’s value.
insertion
insertion
insertion
Deletion

• Given a BST, the task is to delete a node


in this BST, which can be broken down
into 3 scenarios:
Case 1. Delete a Leaf Node in BST:
Case 2. Delete a Node with Single Child in BST

• Copy the child to the node and delete


the node.
Case 3. Delete a Node with Both Children in
BST

Suppose we want to delete node N, which has two


children.
1.Find the node to delete (N).
2.Find the in-order successor (smallest value in the right
subtree) or in-order predecessor (largest in the left
subtree).
3.Copy its value to node N.
4.Delete the successor/predecessor node (which now has
at most one child).
Deletion
Heap Data Structure

• A Heap is a complete binary tree data structure


that satisfies the heap property: for every node,
the value of its children is greater than or equal
to its own value. Heaps are usually used to
implement priority queues, where the smallest
(or largest) element is always at the root of the
tree.
• Hear are two types:
• 1.Max Heap and 2.Min Heap
Max Heap
Insertion in Heaps:

• Process of Insertion: First increase the heap


size by 1, so that it can store the new element.
• Insert the new element at the end of the Heap.
• This newly inserted element may distort the
properties of Heap for its parents. So, in order
to keep the properties of Heap, heapify this
newly inserted element following a bottom-up
approach.
Min Heap
Illustration:
suppose the Heap is a Max-Heap as:
10
/ \
5 3
/ \
2 4
The new element to be inserted is 15.
Process:
Step 1: Insert the new element at the end.
10
/ \
5 3
/ \ /
2 4 15
• Step 2: Heapify the new element
following bottom-up
approach.
-> 15 is more than its parent
3, swap them.
10
/ \
5 15
/ \ /
2 4 3
Illustration:
• -> 15 is again more than its parent 10,
swap them.
15
/ \
5 10
/ \ /
2 4 3
Therefore, the final heap after insertion
is:
15
/ \
5 10
/ \ /
2 4 3
Deletion in Heap:

Process of Deletion:
Since deleting an element at any intermediary
position in the heap can be costly, so we can
simply replace the element to be deleted by the
last element and delete the last element of the
Heap.
• Replace the root or element to be deleted by
the last element.
• Delete the last element from the Heap.
• Since, the last element is now placed at the
position of the root node. So, it may not follow
the heap property. Therefore, heapify the last
node placed at the position of root.
Illustration:
Suppose the Heap is a Max-Heap as:
10
/ \
5 3
/ \
2 4
The element to be deleted is root, i.e. 10.
Process:
The last element is 4.
Step 1: Replace the last element with root, and
delete it.
4
/ \
5 3
/
2
• Step 2: Heapify root.
Final Heap:
5
/ \
4 3
/
2
Huffman Coding

Huffman coding is a lossless data compression


algorithm. The idea is to assign variable-length
codes to input characters, lengths of the
assigned codes are based on the frequencies of
corresponding characters.
Steps to build Huffman Tree
Input is an array of unique characters along with their
frequency of occurrences and output is Huffman Tree.
1.Create a leaf node for each unique character and build
a min heap of all leaf nodes (Min Heap is used as a priority
queue. The value of frequency field is used to compare
two nodes in min heap. Initially, the least frequent
character is at root).
2.Extract two nodes with the minimum frequency from the
min heap.
3. Create a new internal node with a frequency equal to
the sum of the two nodes frequencies. Make the first
extracted node as its left child and the other extracted
node as its right child. Add this node to the min heap.
4.Repeat steps #2 and #3 until the heap contains only one
node. The remaining node is the root node and the tree is
complete.
Example:
character Frequency
a5
b9
c 12
d 13
e 16
f 45
S1:Build a min heap that contains 6 nodes where each
node represents root of a tree with single node.
Huffman Tree
Huffman Tree
Huffman coding
• character code-word
f0
c 100
d 101
a 1100
b 1101
e 111
Graphs

07/14/2024 CS201 73
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
nod
e
edg
e
07/14/2024 CS201 74
Examples of graphs
Molecular Computer
Structure Network
H Server Terminal
1 1
H C H
Terminal
H Server 2
2
Other examples: electrical and communication
networks, airline routes, flow chart, graphs for
planning projects

07/14/2024 CS201 75
Formal Definition of graph
• The set of nodes is denoted as V
• For any nodes u and v, if u and v are
connected by an edge, such edge is denoted
as (u, v) v
(u,
v)
u
• The set of edges is denoted as E
• A graph G is defined as a pair (V, E)

07/14/2024 CS201 76
Adjacent
• Two nodes u and v are said to be adjacent
if (u, v) ∈ E

v
(u,
v)
u
w
u and v are adjacent
v and w are not
adjacent

07/14/2024 CS201 77
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. v v
v
2 3
1
- v2, v3, v4, v2, v1 is a path
v v
- v2, v3, v4, v5 is a path,
also it is a simple path 4
5

07/14/2024 CS201 78
Cycle and simple cycle
• A cycle is a path that begins and ends at
the same node
• A simple cycle is a cycle if every node
appears at most once, except for the first
and the last nodes
v
v v
2
3
- v2, v3, v4, v5 , v3, v2 is a 1

cycle v v
- v2, v3, v4, v2 is a cycle, it is 5
4
also a simple cycle
07/14/2024 CS201 79
Connected graph
• A graph G is connected if there exists path
between every pair of distinct nodes;
otherwise, it is disconnected
v
v v
2
1 3

v v
5
This is a connected
4 graph because there exists
path between every pair of nodes
07/14/2024 CS201 80
Example of disconnected graph

v v v v
1 v 3 7 8

v 2 v
v v
5
4
6
9
This is a disconnected graph because there does
not exist path between some pair of nodes, says,
v1 and v7

07/14/2024 CS201 81
Connected component
• If a graph is disconnect, it can be partitioned into
a number of graphs such that each of them is
connected. Each such graph is called a
connected component.

v v v
v v
2 7 8
1 3

v v
v v
5
4 6
9
07/14/2024 CS201 82
Complete graph
• A graph is complete if each pair of distinct
nodes has an edge

Complete Complete
graph graph
with 3 nodes with 4 nodes

07/14/2024 CS201 83
Subgraph
• A subgraph of a graph G =(V, E) is a graph
H = (U, F) such that U ⊆ V and
F ⊆ E.
v v
v v v
2 2
1 3 3

v v v v
5 5
4 G 4 H

07/14/2024 CS201 84
Weighted graph
• If each edge in G is assigned a weight, it is
called a weighted graph

Chicago 100 New York


0
350
200 0
0

Housto
n
07/14/2024 CS201 85
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
Chicago 100 New York
0
Directed
200 edge
350
0
0

Housto
07/14/2024 n CS201 86
More on directed graph
x y

• If (x, y) is a directed edge, we say


– y is adjacent to x
– y is successor of x
– x is predecessor of y
• In a directed graph, directed path, directed
cycle can be defined similarly

07/14/2024 CS201 87
Multigraph
• A graph cannot have duplicate edges.
• Multigraph allows multiple edges and self
edge (or loop).

Self Multiple
edge edge

07/14/2024 CS201 88
Property of graph
• A undirected graph that is connected and
has no cycle is a tree.
• A tree with n nodes have exactly n-1
edges.
• A connected undirected graph with n
nodes must have at least n-1 edges.

07/14/2024 CS201 89
Implementing Graph
• Adjacency matrix
– Represent a graph using a two-dimensional
array
• Adjacency list
– Represent a graph using n linked lists where
n is the number of vertices

07/14/2024 CS201 90
Adjacency matrix for directed graph
Matrix[i][j] = 1 if 1 2 3 4 5
(vi, vj)∈E
0 if (vi, v1 v2 v3 v4 v5
vj)∉E 1 v1 0 1 0 0 0
v
v v 2 v 0 0 0 1 0
2 2
3
1 3 v3 0 1 0 1 0
v v 4 v4 0 0 0 0 0
5 5 v5 0 0 1 1 0
4 G

07/14/2024 CS201 91
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
v v1 v2 v3 v4 v5
v 2 v
5 2 1 v1 ∞ 5 ∞ ∞ ∞
1 4 3 3
7 2 v2 5 ∞ 2 4 ∞
v
8 v
3 v3 0 2 ∞ 3 7
5
4 G 4 v4 ∞ 4 3 ∞ 8
5 v5 ∞ ∞ 7 8 ∞
07/14/2024 CS201 92
Adjacency list for directed graph

1 v1 → v2
v 2 v2 → v4
v v
2 3 v3 → v2 → v4
1 3
4 v4
v v
5 v5 → v3 → v4
5
4 G

07/14/2024 CS201 93
Adjacency list for weighted
undirected graph

v
v 2 v 1 v1 → v2(5)
5 2
4 3 2 v2 → v1(5) → v3(2) → v4(4)
1 3 7
3 v3 → v2(2) → v4(3) → v5(7)
v
8 v 4 v4 → v2(4) → v3(3) → v5(8)
5 5 v5 → v3(7) → v4(8)
4 G

07/14/2024 CS201 94
Adjacency Matrix
•Definition: A 2D array matrix[V][V] where
matrix[i][j] = 1 (or the weight, in weighted
graphs) if there is an edge from vertex i to vertex j.
Pros:
•Fast edge lookup: O(1) time to check if an edge
exists between two nodes.
•Simple implementation.
•Good for dense graphs (i.e., many edges).

07/14/2024 CS201 95
Cons:

•High space usage: Always takes O(V²)


space, even if the graph has few edges.
•Slow iteration over neighbors: You need
to scan all V entries for a node.
Use When:
•The graph is dense (many edges).
•You need frequent edge existence
checks.
•The number of vertices is small and space
isn't an issue
Adjacency List
•Definition: An array of lists. Each element list[i] contains all vertices
adjacent to vertex i.
Pros:
•Efficient space usage: Takes O(V + E) space.
•Faster neighbor iteration: Only stores actual connections.
•Good for sparse graphs.

Cons:
•Slower edge lookup: O(degree) time to check if an edge exists
between two nodes.
•Slightly more complex to implement.
Use When:
•The graph is sparse (few edges).
•You need to traverse neighbors frequently (like in DFS/BFS).
•Space efficiency is important.
Problems related to Graph
• Graph Traversal
• Topological Sort
• Spanning Tree
• Minimum Spanning Tree
• Shortest Path

07/14/2024 CS201 98
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.

07/14/2024 CS201 99
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

07/14/2024 CS201 100


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.

Algorithm dfs(v)
print v; // you can do other things!
mark v as visited;
for (each unvisited node u adjacent to v)
dfs(u);

07/14/2024 CS201 101


Non-recursive version of DFS
algorithm
Algorithm dfs(v)
s.createStack();
s.push(v);
mark v as visited;
while (!s.isEmpty()) {
let x be the node on the top of the stack s;
if (no unvisited nodes are adjacent to x)
s.pop(); // blacktrack
else {
select an unvisited node u adjacent to x;
s.push(u);
mark u as visited;
}
}
07/14/2024 CS201 102
DFS
As C does not have any unvisited adjacent node so we
keep popping the stack until we find a node that has an
unvisited adjacent node. In this case, there's none and
we keep popping until the stack is empty.
Non-recursive DFS example
visit stack
v3 v3
v
v2 v 3, v 2
v v
v1 v 3, v 2, v 1
x x
2 x3
1
backtrack v 3, v 2
v4 v 3, v 2, v 4
v
x x v
v5 v 3, v 2, v 4 , v 5
5
4
backtrack v 3, v 2, v 4
backtrack v 3, v 2 G
backtrack v3
backtrack empty
07/14/2024 CS201 105
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.

07/14/2024 CS201 106


Algorithm for BFS
Algorithm bfs(v)
q.createQueue();
q.enqueue(v);
mark v as visited;
while(!q.isEmpty()) {
w = q.dequeue();
for (each unvisited node u adjacent to w) {
q.enqueue(u);
mark u as visited;
}
}

07/14/2024 CS201 107


BFS
• At this stage, we are left with no
unmarked (unvisited) nodes. But as per
the algorithm we keep on dequeuing in
order to get all unvisited nodes. When the
queue gets emptied, the program is over.
Spanning Tree

For an undirected, connected graph G = (V, E) with |V| =


n
vertices, a spanning tree is a sub-graph T ⊆ G that
1.Includes every vertex (|V(T)| = n),
2.Is a tree—i.e., it is connected and acyclic,
3.Has exactly n – 1 edges (the minimum needed to connect
n vertices).
If the original graph is disconnected, each component has
its own spanning tree; the union is called a spanning forest.
Spanning tree is not
unique!
Minimum Spanning Tree
• Consider a connected undirected graph where
– Each node x represents a country x
– Each edge (x, y) has a number which measures the
cost of placing telephone line between country x and
country y
• Problem: connecting all countries while
minimizing the total cost
• Solution: find a spanning tree with minimum total
weight, that is, minimum spanning tree

07/14/2024 CS201 111


Formal definition of minimum
spanning tree
• Given a connected undirected graph G.
• Let T be a spanning tree of G.
• cost(T) = ∑e∈Tweight(e)
• The minimum spanning tree is a spanning tree T
which minimizes cost(T)
v
v 2 v
5 Minimu
2
1 4 3 3 m
7
spannin
v
8 v g
5
tree
07/14/2024 CS201
4 112
Prim’s Algorithm
Prim’s Algorithm is used to find a Minimum Spanning Tree
(MST) from a connected, weighted, undirected graph.
•Start from any node.
•At each step, add the smallest weight edge that
connects a visited vertex to an unvisited vertex.
•Repeat until all vertices are included in the tree.

Using min-heap + adjacency list O(E log V)

Using adjacency matrix (naive) O(V²)


Prim’s algorithm (I)
v v v
v 2 v v 2 v v 2 v
5 2 5 2 5 2
1
4 3 1
4 3 1
4 3
73 73 73
v 8 v v 8 v v 8 v
Start from
4 v5, find the 5 Find4the minimum 5 Find4the minimum 5
minimum edge attach to edge attach to v3 edge attach to v2, v3
v5 and v5 and v5

v v v
v 2 v 2 v
5 2
5 2
1
4 3
1
4 3 73
73 8
v 8 v v v
4 5 4 5
Find the minimum edge
attach to v2, v3 , v4 and
v5
07/14/2024 CS201 115
Prim’s algorithm (II)
Algorithm PrimAlgorithm(v)
• Mark node v as visited and include it in the
minimum spanning tree;
• while (there are unvisited nodes) {
– find the minimum edge (v, u) between a visited node v
and an unvisited node u;
– mark u as visited;
– add both v and (v, u) to the minimum spanning tree;
}

07/14/2024 CS201 116


Kruskal’s Algorithm
Finding a Minimum Spanning Tree (MST) in
a weighted, undirected graph.
Greedy algorithm that adds the smallest
edge to the spanning tree without forming a
cycle.
Steps
• Sort all the edges in a non-decreasing
order of their weight.
• Pick the smallest edge. Check if it forms a
cycle with the spanning tree formed so
far. If the cycle is not formed, include this
edge. Else, discard it.
• Repeat step 2 until there are (V-1) edges
in the spanning tree.
Shortest path
• Consider a weighted directed graph
– Each node x represents a city x
– Each edge (x, y) has a number which represent the
cost of traveling from city x to city y
• Problem: find the minimum cost to travel from
city x to city y
• Solution: find the shortest path from x to y

07/14/2024 CS201 120


Formal definition of shortest
path
• Given a weighted directed graph G.
• Let P be a path of G from x to y.
• cost(P) = ∑e∈Pweight(e)
• The shortest path is a path P which minimizes
cost(P)
v
v 2 v
5 2
1 4 3 3 Shortest
4
Path
v
8 v
5
4
07/14/2024 CS201 121
Dijkstra’s algorithm
• Consider a graph G, each edge (u, v) has
a weight w(u, v) > 0.
• Suppose we want to find the shortest path
starting from v1 to any node vi
• Let VS be a subset of nodes in G
• Let cost[vi] be the weight of the shortest
path from v1 to vi that passes through
nodes in VS only.

07/14/2024 CS201 122


Example for Dijkstra’s algorithm
v v 2 v
5
1
2 4 3 3
4
v
8 v
v VS cost[v 5 3]
4 1] cost[v2] cost[v cost[v4] cost[v5]

1 [v1] 0 5 ∞ ∞ ∞

07/14/2024 CS201 123


Example for Dijkstra’s algorithm
v2
v1 v3
5
4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]

1 [v1] 0 5 ∞ ∞ ∞

2 v2 [v1, v2] 0 5 ∞ 9 ∞

07/14/2024 CS201 124


Example for Dijkstra’s algorithm
v2
v 2 v3
5
1 4 3 4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]

1 [v1] 0 5 ∞ ∞ ∞

2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17

07/14/2024 CS201 125


Example for Dijkstra’s algorithm
v2
v1 2 v
5
4 3 3
4
v4
8 v5
v VS cost[v1] cost[v2] cost[v3] cost[v4] cost[v5]

1 [v1] 0 5 ∞ ∞ ∞

2 v2 [v1, v2] 0 5 ∞ 9 ∞
3 v4 [v1, v2, v4] 0 5 12 9 17
4 v3 [v1, v2, v4, v3] 0 5 12 9 16
5 v5 [v1, v2, v4, v3, v5] 0 5 12 9 16

07/14/2024 CS201 126


Dijkstra’s algorithm
Algorithm shortestPath()
n = number of nodes in the graph;
for i = 1 to n
cost[vi] = w(v1, vi);
VS = { v1 };
for step = 2 to n {
find the smallest cost[vi] s.t. vi is not in VS;
include vi to VS;
for (all nodes vj not in VS) {
if (cost[vj] > cost[vi] + w(vi, vj))
cost[vj] = cost[vi] + w(vi, vj);
}
}

07/14/2024 CS201 127


Summary
• Graphs can be used to represent many real-life
problems.
• There are numerous important graph algorithms.
• We have studied some basic concepts and
algorithms.
– Graph Traversal
– Topological Sort
– Spanning Tree
– Minimum Spanning Tree
– Shortest Path

07/14/2024 CS201 128

You might also like