Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
97 views

Learning Module: Algorithm and Complexity (AL101)

This learning module covers algorithm analysis topics including divide and conquer, the max-min problem, and merge sort. It discusses divide and conquer as an approach that divides a problem into smaller subproblems, solves the subproblems independently, and then merges the solutions. The max-min problem involves finding the maximum and minimum values in an array, which can be solved via a naive approach in O(n) time or improved to O(logn) time using divide and conquer. Merge sort is also presented as an example of a divide and conquer algorithm that runs in O(nlogn) time by dividing an array in half, sorting each half recursively, and then merging the sorted halves.

Uploaded by

Ruben San Felipe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Learning Module: Algorithm and Complexity (AL101)

This learning module covers algorithm analysis topics including divide and conquer, the max-min problem, and merge sort. It discusses divide and conquer as an approach that divides a problem into smaller subproblems, solves the subproblems independently, and then merges the solutions. The max-min problem involves finding the maximum and minimum values in an array, which can be solved via a naive approach in O(n) time or improved to O(logn) time using divide and conquer. Merge sort is also presented as an example of a divide and conquer algorithm that runs in O(nlogn) time by dividing an array in half, sorting each half recursively, and then merging the sorted halves.

Uploaded by

Ruben San Felipe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

NORZAGARAY COLLEGE

Municipal Compound, Norzagaray, Bulacan

LEARNING MODULE
IN
ALGORITHM AND
COMPLEXITY
(AL101)
MIDTERM
1ST SEMESTER A.Y. 2020-2021

FELIPE G. ANTE JR
CS Instructor

COLLEGE OF COMPUTING STUDIES

This learning module is for teaching and learning purposes only.


No part of this learning module shall be printed, electronically or
mechanically reproduced, and shared publicly to any form of
social media/online platforms. Anyone who will be directly and
indirectly involved shall be administratively dealt with.
TABLE OF CONTENTS

 LEARNING TASK 6-7..................................................................................................... 2


 DIVIDE & CONQUER ......................................................................................... 2
 MAX-MIN PROBLEM .......................................................................................... 2
 MERGE SORT.................................................................................................... 2
 ACTIVITY 6-7 ................................................................................................................. 2
 PRE-ASSESSMENT 6-7 ................................................................................................ 2
 CONTENT DEVELOPMENT 6-7 .................................................................................... 2
 POST-ASSESSMENT 6-7 .............................................................................................. 6
 LEARNING TASK 8-9..................................................................................................... 6
 BINARY SEARCH............................................................................................... 6
 STRASSEN’S MATRIX MULTIPLICATION ......................................................... 6
 GREEDY METHOD ............................................................................................ 6
 DYNAMIC PROGRAMMING ............................................................................... 6
 ACTIVITY 8-9 ................................................................................................................. 6
 PRE-ASSESSMENT 8-9 ................................................................................................ 6
 CONTENT DEVELOPMENT 8-9 .................................................................................... 7
 POST-ASSESSMENT 8-9 .............................................................................................10
 LEARNING TASK 10 .....................................................................................................11
 SPANNING TREE..............................................................................................11
 SHORTEST PATHS ..........................................................................................11
 MULTISTAGE GRAPH ......................................................................................11
 TRAVELLING SALESMAN PROBLEM ..............................................................11
 PRE-ASSESSMENT 10 ................................................................................................11
 CONTENT DEVELOPMENT 10 ....................................................................................11
 POST-ASSESSMENT 10 ..............................................................................................16
 MIDTERM EXAMINATION ............................................................................................16

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 1
LEARNING TASK 6-7 (April 12-24,2021)

TOPICS
 DIVIDE & CONQUER
 MAX-MIN PROBLEM
 MERGE SORT

TOPIC OVERVIEW

 Learning Task 6-7 will discuss Divide & Conquer, Max-Min Problem and Merge Sort

DESIRED LEARNING OUTCOME

1. Identify the complexities in Divide and Conquer and Max-Min Problem n Design Strategies
2. Identify the complexities in Merge Sort in Design Strategies
-------------------------------------------------------------------------------------------------------------------------------

ACTIVITY 6-7

NAME: _________________________________ COURSE & SECTION: _____SCORE: _____

Answer the following questions. Use a yellow paper. 30 pts.

1. In your own words define the problem in Divide and Conquer


2. What is Max-Min problem, identify its complexities
3. In your own words what is Merge Sort in design strategies of algorithm.

-------------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 6-7

NAME: _______________________________ COURSE & SECTION: ______ SCORE: _____

Answer the following questions:


(Write your answer in a separate sheet of paper) 10pts each

1. What is Divide and Conquer in your own words.


2. What is the best programming language to use when it comes to solving a Math
problem. Explain
3. Based on question 2 create an algorithm for solving a Math problem.
Example: Algorithm of Energy Relationships for Satellites.

-------------------------------------------------------------------------------------------------------------------------------

CONTENT DEVELOPMENT 6-7

I. DIVIDE AND CONQUER


In divide and conquer approach, the problem in hand, is divided into smaller sub-problems
and then each problem is solved independently. When we keep on dividing the subproblems into
even smaller sub-problems, we may eventually reach a stage where no more division is possible.
Those "atomic" smallest possible sub-problem (fractions) are solved. The solution of all
sub-problems is finally merged in order to obtain the solution of an original problem.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 2
Broadly, we can understand divide-and-conquer approach in a three-step process.
Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems should
represent a part of the original problem. This step generally takes a recursive approach to divide
the problem until no sub-problem is further divisible. At this stage, sub-problems become atomic
in nature but still represent some part of the actual problem.

Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the
problems are considered 'solved' on their own.

Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them until
they formulate a solution of the original problem. This algorithmic approach works recursively and
conquer & merge steps works so close that they appear as one.
Examples
The following computer algorithms are based on divide-and-conquer programming approach −
 Merge Sort
 Quick Sort
 Binary Search
 Strassen's Matrix Multiplication
 Closest pair (points)
There are various ways available to solve any computer problem, but the mentioned are a
good example of divide and conquer approach.

II. MAX-MIN PROBLEM

Problem Statement
The Max-Min Problem in algorithm analysis is finding the maximum and minimum value
in an array.
Solution
To find the maximum and minimum numbers in a given array numbers[] of size n, the
following algorithm can be used. First we are representing the naive method and then we will
present divide and conquer approach.

Naïve Method
Naïve method is a basic method to solve any problem. In this method, the maximum and
minimum number can be found separately. To find the maximum and minimum numbers, the
following straightforward algorithm can be used.
Algorithm: Max-Min-Element (numbers[])
max := numbers[1]
min := numbers[1]

for i = 2 to n do
if numbers[i] > max then
max := numbers[i]
if numbers[i] < min then
min := numbers[i]
return (max, min)
Analysis

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 3
The number of comparison in Naive method is 2n - 2.
The number of comparisons can be reduced using the divide and conquer approach. Following
is the technique.

Divide and Conquer Approach


In this approach, the array is divided into two halves. Then using recursive approach
maximum and minimum numbers in each halves are found. Later, return the maximum of two
maxima of each half and the minimum of two minima of each half.
In this given problem, the number of elements in an array is y−x+1y−x+1, where y is greater than
or equal to x.
Max−Min(x,y)Max−Min(x,y) will return the maximum and minimum values of an
array numbers[x...y]numbers[x...y].
Algorithm: Max - Min(x, y)
if y – x ≤ 1 then
return (max(numbers[x], numbers[y]), min((numbers[x], numbers[y]))
else
(max1, min1):= maxmin(x, ⌊((x + y)/2)⌋)
(max2, min2):= maxmin(⌊((x + y)/2) + 1)⌋,y)
return (max(max1, max2), min(min1, min2))
Analysis
Let T(n) be the number of comparisons made by Max−Min(x,y)Max−Min(x,y), where the
number of elements n=y−x+1n=y−x+1.
If T(n) represents the numbers, then the recurrence relation can be represented as
T(n)=⎧⎩⎨⎪⎪T(⌊n2⌋)+T(⌈n2⌉)+210forn>2forn=2forn=1T(n)={T(⌊n2⌋)+T(⌈n2⌉)+2forn>21forn=20for
n=1
Let us assume that n is in the form of power of 2. Hence, n = 2k where k is height of the
recursion tree.

So,
T(n)=2.T(n2)+2=2.(2.T(n4)+2)+2.....=3n2−2T(n)=2.T(n2)+2=2.(2.T(n4)+2)+2.....=3n2−2
Compared to Naïve method, in divide and conquer approach, the number of comparisons is less.
However, using the asymptotic notation both of the approaches are represented by O(n).

III. MERGE SORT

Merge sort is a sorting technique based on divide and conquer technique. With worst-
case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays of
size 4.

This does not change the sequence of appearance of items in the original. Now we divide these
two arrays into halves.

We further divide these arrays and we achieve atomic value which can no more be divided.

Now, we combine them in exactly the same manner as they were broken down. Please note the
color codes given to these lists.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 4
We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target
list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42
and 44 are placed sequentially.

In the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.

After the final merging, the list should look like this −

Now we should learn some programming aspects of merge sorting.


Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller
sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.

Pseudocode
We shall now see the pseudocodes for merge sort functions. As our algorithms point out two
main functions − divide & merge.
Merge sort works with recursion and we shall see our implementation in the same way.

procedure mergesort( var a as array )


if ( n == 1 ) return a

var l1 as array = a[0] ... a[n/2]


var l2 as array = a[n/2+1] ... a[n]

l1 = mergesort( l1 )
l2 = mergesort( l2 )

return merge( l1, l2 )


end procedure

procedure merge( var a as array, var b as array )

var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while

while ( a has elements )


add a[0] to the end of c
remove a[0] from a
end while

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 5
while ( b has elements )
add b[0] to the end of c
remove b[0] from b
end while

return c

end procedure
-------------------------------------------------------------------------------------------------------------------------------

POST-ASSESSMENT 6-7

NAME: _______________________________ COURSE & SECTION: ______ SCORE: _____

Answer the following questions:


(Write your answer in a separate sheet of paper) 15 pts each

1. Create an algorithm for the following Design Strategies and show the flowchart
 Merge Sort
 Quick Sort
 Binary Search
2. Using C# create a sample program for Merge Sort. Show the program output.

------------------------------------------------------------------------------------------------------------------------------

LEARNING TASK 8-9 (April 26-May 8, 2021)

TOPICS
 BINARY SEARCH
 STRASSEN’S MATRIX MULTIPLICATION
 GREEDY METHOD
 DYNAMIC PROGRAMMING

TOPIC OVERVIEW

Learning Task 8-9 focuses on other important design strategies such as binary search,
Strassen’s Matrix Multiplication, Greedy Method and Dynamic Programming

DESIRED LEARNING OUTCOMES:

1. Identify the problems and methods applied in design strategies


2. Analyze the different design strategies in algorithm
-------------------------------------------------------------------------------------------------------------------------------

ACTIVITY 8-9

NAME: _________________________________ COURSE & SECTION: _____SCORE: _____

Directions: Answer the questions on a separate sheet(s). 30 pts


Your Tasks:
1. Create an algorithm for sorting numbers from 100 to 1000
2. Only Even numbers should be sort, example 100,102, 104,106…
3. Show the flowchart.

-----------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 8-9

NAME: _________________________________________ COURSE & SECTION: _________

Answer the following questions on a separate sheet of paper. 15pts each.

1. In your own words what is Binary Search? Explain.


2. In your own words what is Greedy Method? Explain.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 6
-------------------------------------------------------------------------------------------------------------------------------

CONTENT DEVELOPMENT 8-9

I. BINARY SEARCH

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer. For this algorithm to work properly, the
data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is greater than
the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the
item is searched for in the sub-array to the right of the middle item. This process continues on
the sub-array as well until the size of the subarray reduces to zero.
How Binary Search Works?
For a binary search to work, it is mandatory for the target array to be sorted. We shall
learn the process of binary search with a pictorial example. The following is our sorted array and
let us assume that we need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find
that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we
have a sorted array, so we also know that the target value must be in the upper portion of the
array.

We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match, rather it is more than what we are looking for. So,
the value must be in the lower part from this location.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find that it is a match.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 7
We conclude that the target value 31 is stored at location 5.
Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers.
Pseudocode
The pseudocode of binary search algorithms should look like this −

Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched

Set lowerBound = 1
Set upperBound = n

while x not found


if upperBound < lowerBound
EXIT: x does not exists.

set midPoint = lowerBound + ( upperBound - lowerBound ) / 2

if A[midPoint] < x
set lowerBound = midPoint + 1

if A[midPoint] > x
set upperBound = midPoint - 1

if A[midPoint] = x
EXIT: x found at location midPoint
end while

end procedure

II. STRASSEN’S MATRIX MULTIPLICATION

Problem Statement
Let us consider two matrices X and Y. We want to calculate the resultant matrix Z by
multiplying X and Y.

Naïve Method
First, we will discuss naïve method and its complexity. Here, we are calculating Z = X × Y.
Using Naïve method, two matrices (X and Y) can be multiplied if the order of these matrices are p
× q and q × r. Following is the algorithm.
Algorithm: Matrix-Multiplication (X, Y, Z)
for i = 1 to p do
for j = 1 to r do
Z[i,j] := 0
for k = 1 to q do
Z[i,j] := Z[i,j] + X[i,k] × Y[k,j]

Complexity
Here, we assume that integer operations take O(1) time. There are three for loops in this
algorithm and one is nested in other. Hence, the algorithm takes O(n3) time to execute.

Strassen’s Matrix Multiplication Algorithm


In this context, using Strassen’s Matrix multiplication algorithm, the time consumption can
be improved a little bit.
Strassen’s Matrix multiplication can be performed only on square matrices where n is
a power of 2. Order of both of the matrices are n × n.
Divide X, Y and Z into four (n/2)×(n/2) matrices as represented below −
Z=[IKJL]Z=[IJKL] X=[ACBD]X=[ABCD] and Y=[EGFH]Y=[EFGH]

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 8
Using Strassen’s Algorithm compute the following −
M1:=(A+C)×(E+F)M1:=(A+C)×(E+F)
M2:=(B+D)×(G+H)M2:=(B+D)×(G+H)
M3:=(A−D)×(E+H)M3:=(A−D)×(E+H)
M4:=A×(F−H)M4:=A×(F−H)
M5:=(C+D)×(E)M5:=(C+D)×(E)
M6:=(A+B)×(H)M6:=(A+B)×(H)
M7:=D×(G−E)M7:=D×(G−E)
Then,
I:=M2+M3−M6−M7I:=M2+M3−M6−M7
J:=M4+M6J:=M4+M6
K:=M5+M7K:=M5+M7
L:=M1−M3−M4−M5L:=M1−M3−M4−M5
Analysis
T(n)={c7xT(n2)+dxn2ifn=1otherwiseT(n)={cifn=17xT(n2)+dxn2otherwise where c and d a
re constants
Using this recurrence relation, we get T(n)=O(nlog7)T(n)=O(nlog7)
Hence, the complexity of Strassen’s matrix multiplication algorithm is O(nlog7)O(nlog7).

III. GREEDY METHOD

Greedy algorithms build a solution part by part, choosing the next part in such a way, that
it gives an immediate benefit. This approach never reconsiders the choices taken previously. This
approach is mainly used to solve optimization problems. Greedy method is easy to implement
and quite efficient in most of the cases. Hence, we can say that Greedy algorithm is an algorithmic
paradigm based on heuristic that follows local optimal choice at each step with the hope of finding
global optimal solution.
In many problems, it does not produce an optimal solution though it gives an approximate
(near optimal) solution in a reasonable time.

Components of Greedy Algorithm


Greedy algorithms have the following five components −
A candidate set − A solution is created from this set.
A selection function − Used to choose the best candidate to be added to the solution.
A feasibility function − Used to determine whether a candidate can be used to contribute to
the solution.
An objective function − Used to assign a value to a solution or a partial solution.
A solution function − Used to indicate whether a complete solution has been reached.

Areas of Application
Greedy approach is used to solve many problems, such as
 Finding the shortest path between two vertices using Dijkstra’s algorithm.
 Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.

Where Greedy Approach Fails


In many problems, Greedy algorithm fails to find an optimal solution, moreover it may
produce a worst solution. Problems like Travelling Salesman and Knapsack cannot be solved
using this approach.

IV. DYNAMIC PROGRAMMING


Dynamic Programming is also used in optimization problems. Like divide-and-conquer
method,
Dynamic Programming solves problems by combining the solutions of subproblems.
Moreover, Dynamic Programming algorithm solves each sub-problem just once and then saves
its answer in a table, thereby avoiding the work of re-computing the answer every time.
Two main properties of a problem suggest that the given problem can be solved using
Dynamic Programming. These properties are overlapping sub-problems and optimal
substructure.
Overlapping Sub-Problems
Similar to Divide-and-Conquer approach, Dynamic Programming also combines solutions to sub-
problems. It is mainly used where the solution of one sub-problem is needed repeatedly. The

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 9
computed solutions are stored in a table, so that these don’t have to be re-computed. Hence,
this technique is needed where overlapping sub-problem exists.
For example, Binary Search does not have overlapping sub-problem. Whereas recursive
program of Fibonacci numbers have many overlapping sub-problems.
Optimal Sub-Structure
A given problem has Optimal Substructure Property, if the optimal solution of the given
problem can be obtained using optimal solutions of its sub-problems. For example, the Shortest
Path problem has the following optimal substructure property
If a node x lies in the shortest path from a source node u to destination node v, then the
shortest path from u to v is the combination of the shortest path from u to x, and the shortest
path from x to v.
The standard All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-Ford are
typical examples of Dynamic Programming.
Steps of Dynamic Programming Approach
Dynamic Programming algorithm is designed using the following four steps −
 Characterize the structure of an optimal solution.
 Recursively define the value of an optimal solution.
 Compute the value of an optimal solution, typically in a bottom-up fashion.
 Construct an optimal solution from the computed information.

Applications of Dynamic Programming Approach


 Matrix Chain Multiplication
 Longest Common Subsequence
 Travelling Salesman Problem

------------------------------------------------------------------------------------------------------------------------------

POST-ASSESSMENT 8-9
NAME: ____________________________ COURSE & SECTION: ______ SCORE: ____

Answer the ff questions

1. Differentiate Dynamic Programming from Object Oriented Programming.(10 pts)

2. The code below uses Greedy Method, explain the code and explain why it is a Greedy
Method/Approach (15 pts)

using namespace std;


class TIME
{
public:
int hours;
public: TIME()
{
hours = 0;
}
};

3. Create a program for this Dynamic Programming Approach (15 pts)

Initial value of Power A = 20


Initial value of Power B = 8
Area X (3, 2) : If you step into Area X,
A increases by 3,
B increases by 2
Area Y (-5, -10) : If you step into Area Y,
A decreases by 5,
B decreases by 10
Area Z (-20, 5) : If you step into Area Z,

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 10
A decreases by 20,
B increases by 5

4. Describe Travelling Salesman Problem (10 pts)

-------------------------------------------------------------------------------------------------------------------------------

LEARNING TASK 10 (May 10-15, 2021)

TOPICS

 SPANNING TREE
 SHORTEST PATHS
 MULTISTAGE GRAPH
 TRAVELLING SALESMAN PROBLEM

TOPIC OVERVIEW

Learning Task 10 will discuss another set of design strategies in algorithm such as
Spanning Tree, Shortest Paths, Multistage graph and Travelling Salesman Problem

DESIRED LEARNING OUTCOME

1. Identify the techniques or methods in solving more complex algorithm design strategies
2. Analyze all the design strategies and apply it in creating a quality product such as software or
firmware.
-------------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 10

NAME: _________________________________________ COURSE & SECTION: _________

Answer the following questions. Use a separate sheet of paper for your answer. 10 pts. each.

1. Create an algorithm to Assess the components of a Personal Computer.


2. Based on your answer in 1 what is the best approach to create this algorithm
3. Based again on your answer in 1, what are the importance of creating that
algorithm

-------------------------------------------------------------------------------------------------------------------------------

CONTENT DEVELOPMENT 10

I. SPANNING TREE
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected.
By this definition, we can draw a conclusion that every connected and undirected Graph
G has at least one spanning tree. A disconnected graph does not have any spanning tree, as it
cannot be spanned to all its vertices.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 11
We found three spanning trees off one complete graph. A complete undirected graph can
have maximum nn-2 number of spanning trees, where n is the number of nodes. In the above
addressed example, n is 3, hence 33−2 = 3 spanning trees are possible.
General Properties of Spanning Tree
We now understand that one graph can have more than one spanning tree. Following are a
few properties of the spanning tree connected to graph G −
 A connected graph G can have more than one spanning tree.
 All possible spanning trees of graph G, have the same number of edges and vertices.
 The spanning tree does not have any cycle (loops).
 Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.
 Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree
is maximally acyclic.

Mathematical Properties of Spanning Tree


 Spanning tree has n-1 edges, where n is the number of nodes (vertices).
 From a complete graph, by removing maximum e - n + 1 edges, we can construct a
spanning tree.
 A complete graph can have maximum nn-2 number of spanning trees.
Thus, we can conclude that spanning trees are a subset of connected Graph G and disconnected
graphs do not have spanning tree.
Application of Spanning Tree
Spanning tree is basically used to find a minimum path to connect all nodes in a graph.
Common application of spanning trees are −
 Civil Network Planning
 Computer Network Routing Protocol
 Cluster Analysis
Let us understand this through a small example. Consider, city network as a huge graph and
now plans to deploy telephone lines in such a way that in minimum lines we can connect to all
city nodes. This is where the spanning tree comes into picture.

Minimum Spanning Tree (MST)


In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight than
all other spanning trees of the same graph. In real-world situations, this weight can be measured
as distance, congestion, traffic load or any arbitrary value denoted to the edges.

II. SHORTEST PATHS

Shortest path algorithms have various uses, most notable being Route planning
software such as Google Maps, and us – here at MyRouteOnline we make use of shortest path
algorithms to generate your routes.
As there are a number of different shortest path algorithms, we’ve gathered the most important to
help you understand how they work and which is the best.
Dijkstra’s Algorithm
Dijkstra’s Algorithm stands out from the rest due to its ability to find the shortest path from
one node to every other node within the same graph data structure. This means, that rather than
just finding the shortest path from the starting node to another specific node, the algorithm works
to find the shortest path to every single reachable node – provided the graph doesn’t change.
The algorithm runs until all of the reachable nodes have been visited. Therefore, you would
only need to run Dijkstra’s algorithm once, and save the results to be used again and again without
re-running the algorithm – again, unless the graph data structure changed in any way.
In the case of a change in the graph, you would need to rerun the graph to ensure you have the
most updated shortest paths for your data structure.
Let’s take our routing example from above, if you want to go from A to B in the shortest way
possible, but you know that some roads are heavily congested, blocked, undergoing works, and

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 12
so on, when using Dijkstra, the algorithm will find the shortest path while avoiding any edges with
larger weights, thereby finding you the shortest route.
Bellman-Ford Algorithm
Similar to Dijkstra’s algorithm, the Bellman-Ford algorithm works to find the shortest path
between a given node and all other nodes in the graph. Though it is slower than the former,
Bellman-Ford makes up for its a disadvantage with its versatility. Unlike Dijkstra’s algorithm,
Bellman-Ford is capable of handling graphs in which some of the edge weights are negative.
It’s important to note that if there is a negative cycle – in which the edges sum to a negative
value – in the graph, then there is no shortest or cheapest path. Meaning the algorithm is
prevented from being able to find the correct route since it terminates on a negative cycle.
Bellman-Ford is able to detect negative cycles and report on their existence.
Floyd-Warshall Algorithm
The Floyd-Warshall stands out in that unlike the previous two algorithms it is not a single-
source algorithm. Meaning, it calculates the shortest distance between every pair of nodes in the
graph, rather than only calculating from a single node. It works by breaking the main problem into
smaller ones, then combines the answers to solve the main shortest path issue.
Floyd-Warshall is extremely useful when it comes to generating routes for multi-stop trips
as it calculates the shortest path between all the relevant nodes. For this reason, many route
planning software’ will utilize this algorithm as it will provide you with the most optimized route
from any given location. Therefore, no matter where you currently are, Floyd-Warshall will
determine the fastest way to get to any other node on the graph.
Johnson’s Algorithm
Johnson’s algorithm works best with sparse graphs – one with fewer edges, as it’s runtime
depends on the number of edges. So, the fewer edges, the faster it will generate a route.
This algorithm varies from the rest as it relies on two other algorithms to determine the
shortest path. First, it uses Bellman-Ford to detect negative cycles and eliminate any negative
edges. Then, with this new graph, it relies on Dijkstra’s algorithm to calculate the shortest paths
in the original graph that was inputted.
Final Note
More often than not, the best algorithm to use won’t be left up to you to decide, rather it
will be dependant upon the type of graph you are using and the shortest path problem that is
being solved. For example, for problems with negative weight edges, you would turn to Bellman-
Ford, whereas for sparse graphs with no negative edges you would turn to Dijsktra’s algorithm.
When it comes down to it, many aspects of these algorithms are the same, however, when you
look at performance and use, that’s where the differences come to light. Therefore, rather than
asking which algorithm is the best, you should consider which is the right one for the type of graph
you are operating on and shortest path problem you are trying to solve.

III. MULTISTAGE GRAPH

A multistage graph G = (V, E) is a directed graph where vertices are partitioned


into k (where k > 1) number of disjoint subsets S = {s1,s2,…,sk} such that edge (u, v) is in E,
then u Є si and v Є s1 + 1 for some subsets in the partition and |s1| = |sk| = 1.
The vertex s Є s1 is called the source and the vertex t Є sk is called sink.
G is usually assumed to be a weighted graph. In this graph, cost of an edge (i, j) is represented
by c(i, j). Hence, the cost of path from source s to sink t is the sum of costs of each edges in this
path.
The multistage graph problem is finding the path with minimum cost from source s to
sink t.
Example
Consider the following example to understand the concept of multistage graph.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 13
According to the formula, we have to calculate the cost (i, j) using the following steps
Step-1: Cost (K-2, j)
In this step, three nodes (node 4, 5. 6) are selected as j. Hence, we have three options to choose
the minimum cost at this step.
Cost(3, 4) = min {c(4, 7) + Cost(7, 9),c(4, 8) + Cost(8, 9)} = 7
Cost(3, 5) = min {c(5, 7) + Cost(7, 9),c(5, 8) + Cost(8, 9)} = 5
Cost(3, 6) = min {c(6, 7) + Cost(7, 9),c(6, 8) + Cost(8, 9)} = 5
Step-2: Cost (K-3, j)
Two nodes are selected as j because at stage k - 3 = 2 there are two nodes, 2 and 3. So, the
value i = 2 and j = 2 and 3.
Cost(2, 2) = min {c(2, 4) + Cost(4, 8) + Cost(8, 9),c(2, 6) +
Cost(6, 8) + Cost(8, 9)} = 8
Cost(2, 3) = {c(3, 4) + Cost(4, 8) + Cost(8, 9), c(3, 5) + Cost(5, 8)+ Cost(8, 9), c(3, 6) + Cost(6,
8) + Cost(8, 9)} = 10
Step-3: Cost (K-4, j)
Cost (1, 1) = {c(1, 2) + Cost(2, 6) + Cost(6, 8) + Cost(8, 9), c(1, 3) + Cost(3, 5) + Cost(5, 8) +
Cost(8, 9))} = 12
c(1, 3) + Cost(3, 6) + Cost(6, 8 + Cost(8, 9))} = 13
Hence, the path having the minimum cost is 1→ 3→ 5→ 8→ 9.

IV. TRAVELLING SALESMAN PROBLEM

Problem Statement
A traveler needs to visit all the cities from a list, where distances between all the cities are
known and each city should be visited just once. What is the shortest possible route that he visits
each city exactly once and returns to the origin city?

Solution
Travelling salesman problem is the most notorious computational problem. We can use
brute-force approach to evaluate every possible tour and select the best one. For n number of
vertices in a graph, there are (n - 1)! number of possibilities.
Instead of brute-force using dynamic programming approach, the solution can be obtained in
lesser time, though there is no polynomial time algorithm.
Let us consider a graph G = (V, E), where V is a set of cities and E is a set of weighted
edges. An edge e(u, v) represents that vertices u and v are connected. Distance between
vertex u and v is d(u, v), which should be non-negative.
Suppose we have started at city 1 and after visiting some cities now we are in city j. Hence,
this is a partial tour. We certainly need to know j, since this will determine which cities are most
convenient to visit next. We also need to know all the cities visited so far, so that we don't repeat
any of them. Hence, this is an appropriate sub-problem.
For a subset of cities S Є {1, 2, 3, ... , n} that includes 1, and j Є S, let C(S, j) be the length
of the shortest path visiting each node in S exactly once, starting at 1 and ending at j.
When |S| > 1, we define C(S, 1) = ∝ since the path cannot start and end at 1.
Now, let express C(S, j) in terms of smaller sub-problems. We need to start at 1 and end at j. We
should select the next city in such a way that
C(S,j)=minC(S−{j},i)+d(i,j)wherei∈Sandi≠jc(S,j)=minC(s−{j},i)+d(i,j)wherei∈Sandi≠jC(S,j)=minC(S
−{j},i)+d(i,j)wherei∈Sandi≠jc(S,j)=minC(s−{j},i)+d(i,j)wherei∈Sandi≠j

Algorithm: Traveling-Salesman-Problem

C ({1}, 1) = 0

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 14
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j Є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)

Analysis
There are at the most 2n.n2n.n sub-problems and each one takes linear time to solve.
Therefore, the total running time is O(2n.n2)O(2n.n2).

Example
In the following example, we will illustrate the steps to solve the travelling salesman problem.

From the above graph, the following table is prepared.

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 15
The minimum cost path is 35.
Start from cost {1, {2, 3, 4}, 1}, we get the minimum value for d [1, 2]. When s = 3, select
the path from 1 to 2 (cost is 10) then go backwards. When s = 2, we get the minimum value for d
[4, 2]. Select the path from 2 to 4 (cost is 10) then go backwards.
When s = 1, we get the minimum value for d [4, 3]. Selecting path 4 to 3 (cost is 9), then
we shall go to then go to s = Φ step. We get the minimum value for d [3, 1] (cost is 6).

POST-ASSESSMENT 10
NAME: ____________________________ COURSE & SECTION: ______ SCORE: ____

Answer the ff questions 10 pts each

1, Illustrate the Spanning Tree of the given graph

2. Create a sample program for Travelling Salesman Problem using C#


3. Based on your program in item 2 create the algorithm
4. Based on your answer in item 1 explain your solution

AL101 Property of Norzagaray College


Introduction to Algorithms

Page | 16
AL101 Property of Norzagaray College
Introduction to Algorithms

Page | 17

You might also like