SlideShare a Scribd company logo
1 of 6
Download to read offline
Green University of Bangladesh
Department of Computer Science and Engineering (CSE)
Faculty of Sciences and Engineering
Semester: (Summer, Year:2022), B.Sc. in CSE (Day)
LAB REPORT NO #02
Course Title: Algorithms Lab
Course Code: CSE 206 Section: 211 DB
Student Details
Name ID
1. Rahul Bhowmick 211902045
Submission Date : 6 September 2022
Course Teacher’s Name : Md. Abu Rumman Refat
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
TITLE OF THE LAB EXPERIMENT
Implement proves that, if there exists a negative edge, Dijkstra’s shortest path
algorithm may fail to find the shortest path. Please add the Bellman-Ford
algorithm.
OBJECTIVE
My aim is to implement on prove that, if there exists a negative edge,
Dijkstra’s shortest path algorithm may fail to find the shortest path. Please
add the Bellman-Ford algorithm.
IMPLEMENTATION
import java.util.ArrayList;
import java.util.List;
class Graph {
private int V;
private List<Edge> edges;
public Graph(int v) {
V = v;
edges = new ArrayList<Edge>();
}
public int getV() {
return V;
}
public void setV(int v) {
V = v;
}
public List<Edge> getEdges() {
return edges;
}
public void setEdges(List<Edge> edges) {
this.edges = edges;
}
public void addEdge(int u, int v, int w) {
Edge e = new Edge(u, v, w);
edges.add(e);
}
}
class Edge {
private int u;
private int v;
private int w;
public int getU() {
return u;
}
public void setU(int u) {
this.u = u;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public int getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public Edge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}
}
public class BellmanFord {
public static void main(String args[]) {
Graph g = createGraph();
int distance[] = new int[g.getV()];
boolean hasNegativeCycle = getShortestPaths(g, 1, distance);
if (!hasNegativeCycle) {
System.out.println("Vertex t: Distance");
for (int i = 1; i < distance.length; i++) {
System.out.println("" + i + " " + " t" + (distance[i] ==
Integer.MAX_VALUE ? "-" : distance[i]));
}
} else {
System.out.println("Negative cycle exists in the graph, no
solution found!!!");
}
}
private static Graph createGraph() {
int v = 5;
Graph g = new Graph(v);
g.addEdge(0, 1, 2);
g.addEdge(0, 2, 4);
g.addEdge(1, 3, 2);
g.addEdge(2, 4, 3);
g.addEdge(2, 3, 4);
g.addEdge(4, 3, -5);
return g;
}
public static boolean getShortestPaths(Graph g, int source, int[]
distance) {
int V = g.getV();
for (int i = 1; i < V; i++) {
distance[i] = Integer.MAX_VALUE;
}
distance[source] = 0;
for (int i = 1; i < V; i++) {
for (Edge e : g.getEdges()) {
int u = e.getU(), v = e.getV(), w = e.getW();
if (distance[u] != Integer.MAX_VALUE && distance[v] >
distance[u] + w) {
distance[v] = distance[u] + w;
}
}
}
for (Edge e : g.getEdges()) {
int u = e.getU(), v = e.getV(), w = e.getW();
if (distance[u] != Integer.MAX_VALUE && distance[v] >
distance[u] + w) {
return true;
}
}
return false;
}
}
OUTPUT
ANALYSIS AND DISCUSSION
● Program is running without any error and showing correct output.
● I have got some problems like not giving punctuation when I write this code.
● All after I have run this code very well with no issues.
● Objectives of all the problems are successfully achieved.
SUMMARY
From this assignment, I have more knowledge about the algorithm of Depth First
Search in the Java program.

More Related Content

Similar to Dijkstra's shortest path algorithm

Comparative Analysis of Algorithms for Single Source Shortest Path Problem
Comparative Analysis of Algorithms for Single Source Shortest Path ProblemComparative Analysis of Algorithms for Single Source Shortest Path Problem
Comparative Analysis of Algorithms for Single Source Shortest Path ProblemCSCJournals
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Adding where to your ruby apps
Adding where to your ruby appsAdding where to your ruby apps
Adding where to your ruby appsRoberto Pepato
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08HUST
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
K means clustering
K means clusteringK means clustering
K means clusteringAhmedasbasb
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task QueueDuy Do
 
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your AppLinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your AppSteven Pousty
 
Technical aptitude test 2 CSE
Technical aptitude test 2 CSETechnical aptitude test 2 CSE
Technical aptitude test 2 CSESujata Regoti
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Umar Farooq
 
FAST MAP PROJECTION ON CUDA.ppt
FAST MAP PROJECTION ON CUDA.pptFAST MAP PROJECTION ON CUDA.ppt
FAST MAP PROJECTION ON CUDA.pptgrssieee
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docxajoy21
 
The Green Lab - [12-A] Data visualization in R
The Green Lab - [12-A] Data visualization in RThe Green Lab - [12-A] Data visualization in R
The Green Lab - [12-A] Data visualization in RGiuseppe Procaccianti
 
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
2017 RM-URISA Track:  Spatial SQL - The Best Kept Secret in the Geospatial World2017 RM-URISA Track:  Spatial SQL - The Best Kept Secret in the Geospatial World
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial WorldGIS in the Rockies
 

Similar to Dijkstra's shortest path algorithm (20)

Comparative Analysis of Algorithms for Single Source Shortest Path Problem
Comparative Analysis of Algorithms for Single Source Shortest Path ProblemComparative Analysis of Algorithms for Single Source Shortest Path Problem
Comparative Analysis of Algorithms for Single Source Shortest Path Problem
 
10.ppt
10.ppt10.ppt
10.ppt
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Adding where to your ruby apps
Adding where to your ruby appsAdding where to your ruby apps
Adding where to your ruby apps
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
CP L1
CP L1CP L1
CP L1
 
Computer Practical XII
Computer Practical XIIComputer Practical XII
Computer Practical XII
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Taller parcial 2
Taller parcial 2Taller parcial 2
Taller parcial 2
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your AppLinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
LinuxFest NW - Using Postgis To Add Some Spatial Flavor To Your App
 
Technical aptitude test 2 CSE
Technical aptitude test 2 CSETechnical aptitude test 2 CSE
Technical aptitude test 2 CSE
 
java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
 
FAST MAP PROJECTION ON CUDA.ppt
FAST MAP PROJECTION ON CUDA.pptFAST MAP PROJECTION ON CUDA.ppt
FAST MAP PROJECTION ON CUDA.ppt
 
Write a program that reads a graph from a file and determines whether.docx
 Write a program that reads a graph from a file and determines whether.docx Write a program that reads a graph from a file and determines whether.docx
Write a program that reads a graph from a file and determines whether.docx
 
The Green Lab - [12-A] Data visualization in R
The Green Lab - [12-A] Data visualization in RThe Green Lab - [12-A] Data visualization in R
The Green Lab - [12-A] Data visualization in R
 
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
2017 RM-URISA Track:  Spatial SQL - The Best Kept Secret in the Geospatial World2017 RM-URISA Track:  Spatial SQL - The Best Kept Secret in the Geospatial World
2017 RM-URISA Track: Spatial SQL - The Best Kept Secret in the Geospatial World
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 

Recently uploaded

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 

Dijkstra's shortest path algorithm

  • 1. Green University of Bangladesh Department of Computer Science and Engineering (CSE) Faculty of Sciences and Engineering Semester: (Summer, Year:2022), B.Sc. in CSE (Day) LAB REPORT NO #02 Course Title: Algorithms Lab Course Code: CSE 206 Section: 211 DB Student Details Name ID 1. Rahul Bhowmick 211902045 Submission Date : 6 September 2022 Course Teacher’s Name : Md. Abu Rumman Refat Lab Report Status Marks: ………………………………… Signature:..................... Comments:.............................................. Date:..............................
  • 2. TITLE OF THE LAB EXPERIMENT Implement proves that, if there exists a negative edge, Dijkstra’s shortest path algorithm may fail to find the shortest path. Please add the Bellman-Ford algorithm. OBJECTIVE My aim is to implement on prove that, if there exists a negative edge, Dijkstra’s shortest path algorithm may fail to find the shortest path. Please add the Bellman-Ford algorithm. IMPLEMENTATION import java.util.ArrayList; import java.util.List; class Graph { private int V; private List<Edge> edges; public Graph(int v) { V = v; edges = new ArrayList<Edge>(); } public int getV() { return V; } public void setV(int v) { V = v; } public List<Edge> getEdges() {
  • 3. return edges; } public void setEdges(List<Edge> edges) { this.edges = edges; } public void addEdge(int u, int v, int w) { Edge e = new Edge(u, v, w); edges.add(e); } } class Edge { private int u; private int v; private int w; public int getU() { return u; } public void setU(int u) { this.u = u; } public int getV() { return v; } public void setV(int v) { this.v = v; } public int getW() { return w; } public void setW(int w) { this.w = w; } public Edge(int u, int v, int w) { this.u = u; this.v = v; this.w = w; } } public class BellmanFord { public static void main(String args[]) {
  • 4. Graph g = createGraph(); int distance[] = new int[g.getV()]; boolean hasNegativeCycle = getShortestPaths(g, 1, distance); if (!hasNegativeCycle) { System.out.println("Vertex t: Distance"); for (int i = 1; i < distance.length; i++) { System.out.println("" + i + " " + " t" + (distance[i] == Integer.MAX_VALUE ? "-" : distance[i])); } } else { System.out.println("Negative cycle exists in the graph, no solution found!!!"); } } private static Graph createGraph() { int v = 5; Graph g = new Graph(v); g.addEdge(0, 1, 2); g.addEdge(0, 2, 4); g.addEdge(1, 3, 2); g.addEdge(2, 4, 3); g.addEdge(2, 3, 4); g.addEdge(4, 3, -5); return g; } public static boolean getShortestPaths(Graph g, int source, int[] distance) { int V = g.getV(); for (int i = 1; i < V; i++) { distance[i] = Integer.MAX_VALUE; } distance[source] = 0; for (int i = 1; i < V; i++) { for (Edge e : g.getEdges()) { int u = e.getU(), v = e.getV(), w = e.getW(); if (distance[u] != Integer.MAX_VALUE && distance[v] > distance[u] + w) { distance[v] = distance[u] + w; } }
  • 5. } for (Edge e : g.getEdges()) { int u = e.getU(), v = e.getV(), w = e.getW(); if (distance[u] != Integer.MAX_VALUE && distance[v] > distance[u] + w) { return true; } } return false; } } OUTPUT
  • 6. ANALYSIS AND DISCUSSION ● Program is running without any error and showing correct output. ● I have got some problems like not giving punctuation when I write this code. ● All after I have run this code very well with no issues. ● Objectives of all the problems are successfully achieved. SUMMARY From this assignment, I have more knowledge about the algorithm of Depth First Search in the Java program.