Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Jannatul Hafsa Mim

ID:201-16-500
Department of CIS
Subject: Algorithm
Spring 2021

FINAL ASSINGMENT

Theory Part
Task 1:
(a) Design an algorithm to find the best route for the problem above. Your algorithm
Should not manually check every edge in the graph, rather it should opt for a
Greedy approach.

Answer to the question No 1(a):


From the question above it has said that we have to design an algorithm that give us the shortest
distance from Mothijheel to Mogbazar by approaching greedily.
We all know that Dijkstra is an algorithm for finding the shortest route from source to certain
destination by travelling the nodes.
The algorithm of Dijkstra is described below.
1) First we create three object or class along with main class. In the main class we will
provide the node and traffic level.
2) The three object is Node, Edge and Graph. The class Node an Edge mobilizes the graph.
3) Then we create graph to integrate the Dijkstra algorithm in it.
4) After creating all the nodes and edges we added nodes to the edges that can take two
nodes that are connected.
5) Then we set a source which is Mothijheel. We assign Mothijheel to 0 as we didn’t take
any string value.
6) Then I implement a for loop for the source to visiting every node.
7) Additionally again I take a for loop around the edges of current node for visiting the
graph.
8) If the neighbor of the current node is not visited then we used a temporary value to store
the distance from source.
9) Afterwards I used a Boolean expression whether the neighbor is visited or not.
10) Then take the shortest distance node as next node.
11) Store that shortest distance.
12) Print the result by creating a print method.
13) Last but most important we will call all the necessary methods in the main class and run
the program.

(b) If you were to visit all the intersections in the graph by crossing the least amount
of traffic instead of finding the shortest route, which algorithm could you use?

Answer to the question No 1(b):

If I were to visit all the intersections in the graph by crossing the least amount of traffic
instead of finding the shortest route, I would use Prim’s algorithm.

There are two algorithm for crossing the graph with the least amount of traffic. One is
edge approach which is Kruskal’s and the other one is vertex approach that is Prim’s
From the graph we can see that there is 14 vertices and 18 edges.
Prim’s algorithm travels by adding the cost of the lowest vertex to the existing vertex. On
the other hand kruskal’s algorithm travels by adding the lowest edge to the existing edge.
So it would be difficult for kruskal’s algorithm to travel so many edges because it
approaches through edge. Therefore in a comparative way it will take more time than
prim’s. for that reason prim’s algorithm will work better because prim’s is faster.
Additionally prim’s will work nicely because our graph has many edges and kruskal’s
will work faster in those graph which has less edges.
Task 2 (Simulation):

(a) Simulate the algorithm you have written for the given test case to find the
Shortest route from Motijheel to Mogbazar.

Answer to the question No 2(a):


(b) Simulate the second algorithm that visits all the nodes with the least cost.

Answer to the question No 2(b):


Task 3 (Critical Evaluation):
(a) Mention any alternate algorithms that might be used to solve the shortest route
problem without resorting to a greedy approach and compare its time complexity
with the algorithm that you used.

Answer to the question No 3(a):


In task 1 I have used Dijkstra’s algorithm to find the shortest route.
Dijkstra’s algorithm is finding the shortest route from a starting node to a targeted node in a
weighted graph.it approaches greedily.
However there’s an alternative algorithm that can solve this problem without resorting to a
greedy approach and that is Bellman-Ford algorithm.it also calculates shortest path from a single
source vertex to all the other vertices. It can be used in both weighted and unweighted graphs.
The difference between Dijkstra and Bellman ford algorithm is that Bellman-Ford is capable of
handling negative weights whereas Dijkstra’s can only handle positive weights.one of its
advantages is that we can use this algorithm to check whether there is a negative cycle or not.
And come to time complexity The Bellman-Ford algorithm has more time complexity than
Dijkstra’s algorithm. Because it does not approach in a greedy way.it simply relaxes all the edges
and does. Dijkstra’s has better time complexity if there are no negative weight edges. The time
complexity of Dijkstra’s is O(V2).
On the contrary time complexity of Bellman-Ford is O(|V||E|) which is more than Dijkstra’s.
V is the number of vertices and E is the number of edges.
Bellman-Ford algorithm takes more time because it checks every vertices and visit a vertex more
than one time but Dijkstra’s only check the minimum distances and check vertices only one time.

Lab Part

Task 1:
Write the Java Code for the problem based on your algorithm.

Answer to that question:


OUTPUT

Task 2:
(a) BFS is an algorithm that also gives the shortest path between source and
destination. Run BFS on the given graph and show the output.

Answer to the question No 2(a):


OUTPUT
(b) Explain why BFS is not guaranteed provide the shortest route in most cases?

Answer to the question No 2(b):


Bfs fails to provide the shortest route in most cases.
Breadth first search is a graph traversing algorithm.it gives the shortest route from source to
destination by traversing all the nodes. Bfs will only work in unweighted graph. Bfs failed to
provide shortest route in weighted graph. The reason it worked in an unweighted graph is that the
cost of all the vertices is equal, which is considered one. But the weighted graph cost may vary.
The cost of travelling through edge can differ from edge to edge. We calculate the shortest path
between two vertices by the sum of the edges whose weights are the least.
Bfs will not work in a weighted graph since the way with the last edges may not be the briefest if
the ages it contains are costly.

Task 3:
If you were to change some of the edge costs in your given graph to negative values,
the greedy approach to the shortest path algorithm would not work. Why would that be?
Explain briefly.

Answer to the question No 3:


Dijkstra’s is an algorithm which gives the shortest path from source vertex. It opt for a greedy
approach; it will not work if the value is negative.
Dijkstra's algorithm uses the unvisited vertex with the least distance, calculates the distance
through it to each unvisited neighbor and changes the neighbor distance if smaller. It mark
checks if we find any vertex with shorter cost. But if we use negative weights we have to go back
to those neighbors nodes which is against Dijkstra's rules. The vertex that mark checked can
never be revisited. To add more if we use negative weights to find shortest route we will stuck in
an endless loop. That’s why we cannot use negative weights on Dijkstra’s algorithm.
Now we will understand more briefly with an example.
First the source A checked the nearest vertex C and B.
Now if we take B is on the top of min-heap queue. We have to update it cost from source which
is 3. So the distance from source A to D is 3+1 = 4.
Now D is on the top of the min-heap, and we checked D. Since 5 - 4 = 1 < 3, distance to B must
be updated to 1. Now all the nodes have been checked.
A→C→B→D has total distance 5 + (-4) + 1 = 2 < 4; thus the length of the shortest path
from the source A to D was not correctly estimated.

You might also like