SlideShare a Scribd company logo
JAVA
Implement the DFS algorithm. The implementation should be able to find the shortest path from
a source to any destination of a directed graph, print both the path and the cost of the path.
Solution
//updated code
import java.util.Scanner;
public class Dij
{
static Scanner scanner
public static void main(String[] args)
{
int[] p = new int[5];
int minimum = 999, nextNode = 0;
scanner = new Scanner(System.in);
int[] distancematrix = new int[5];
int[][] actualmatrix = new int[5][5];
int[] visitedarray = new int[5]; 
System.out.println("Enter the cost matrix");
for (int i = 0; i < distancematrix.length; i++)
{
visitedarray[i] = 0;
p[i] = 0;
for (int j = 0; j < distancematrix.length; j++)
{
actualmatrix[i][j] = scanner.nextInt();
if (actualmatrix[i][j]==0)
actualmatrix[i][j] = 999;
}
}
distancematrix = actualmatrix[0];
visitedarray[0] = 1;
distancematrix[0] = 0;
for (int counter = 0; counter < 5; counter++)
{
min = 999;
for (int i = 0; i < 5; i++)
{
if (minimum > distancematrix[i] && visitedarray[i]!=1)
{
minimum = distancematrix[i];
nextNode = i;
}
}
visitedarray[nextNode] = 1;
for (int i = 0; i < 5; i++)
{
if (visitedarray[i]!=1)
{
if (minimum+actualmatrix[nextNode][i] <
distancematrix[i])
{
distancematrix[i]
=minimum+actualmatrix[nextNode][i];
p[i] = nextNode;
}
}
}
}
for(int i = 0; i < 5; i++)
System.out.print("|" + distancematrix[i]);
System.out.println("|");
int j;
for (int i = 0; i < 5; i++)
{
if (i!=0)
{
System.out.print("Path = " + i);
j = i;
do
{
j = p[j];
System.out.print(" <- " + j);
}
while(j != 0);
}
System.out.println();
}
}
}

More Related Content

PDF
Program To change this license header, choose License Heade.pdf
DOC
algorithm Unit 3
PDF
Dijkstra's shortest path algorithm
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPTX
computer networks lab program Bellman Ford.pptx
DOC
Unit 3 daa
PPTX
Graph theory basics
PDF
Please implement in Java. comments would be appreciated 5.pdf
Program To change this license header, choose License Heade.pdf
algorithm Unit 3
Dijkstra's shortest path algorithm
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
computer networks lab program Bellman Ford.pptx
Unit 3 daa
Graph theory basics
Please implement in Java. comments would be appreciated 5.pdf

Similar to JAVAImplement the DFS algorithm. The implementation should be able.pdf (7)

PPT
9_graphs2.v4.ppt
PPTX
Dynamic Programming in design and analysis .pptx
PDF
#include #include Number of vertices in the graph #define V 9 .pdf
PPT
Graphs
PPTX
Floyd warshall algorithm and it's applications
PPTX
Data structure and algorithm
PPT
Shortest path
9_graphs2.v4.ppt
Dynamic Programming in design and analysis .pptx
#include #include Number of vertices in the graph #define V 9 .pdf
Graphs
Floyd warshall algorithm and it's applications
Data structure and algorithm
Shortest path
Ad

More from ezycolours78 (20)

PDF
Find the rate 32. of change in luxury purchases in China with respect.pdf
PDF
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
PDF
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
PDF
Give short answers for the following questionsi.Define ‘Accountin.pdf
PDF
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
PDF
Discuss how you would go about setting up a human source collection .pdf
PDF
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
PDF
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
PDF
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
PDF
6. Edit the following for the format of a reference listREFERENCE.pdf
PDF
2.Distinguish between assigned and emergent leaders. Give an example.pdf
PDF
3. What lines of evidence do we have that evolution has taken place.pdf
PDF
4. Where in your home would endospores most likely be found Wh b. .pdf
PDF
25. Radium is called a _________ and replaces calcium in bones and c.pdf
PDF
What skills does law enforcement need to combat cyber crimesSol.pdf
PDF
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
PDF
were CPA involved in Enron scandal act with integrity as defined by .pdf
PDF
16) What are the two types of current What is different about the fl.pdf
PDF
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
PDF
To complete the task, you need to fill in the missing code. I’ve inc.pdf
Find the rate 32. of change in luxury purchases in China with respect.pdf
Given f(x) = x^2x^2 + 3 Find (a) the domain and range of f, (b) the.pdf
Help with Starting out with visual basic 7th edition chapter 6 Progr.pdf
Give short answers for the following questionsi.Define ‘Accountin.pdf
For the reaction NO+03--NO2+O2, the reaction was found to be first .pdf
Discuss how you would go about setting up a human source collection .pdf
Describe how meiotic and mitotic nondisjunctions occur and their pos.pdf
CASE 16 Southwest Airlines Andrew Inkpen and ex.pdf
An epic bribe scandal at Petrobras, a major oil company controlled b.pdf
6. Edit the following for the format of a reference listREFERENCE.pdf
2.Distinguish between assigned and emergent leaders. Give an example.pdf
3. What lines of evidence do we have that evolution has taken place.pdf
4. Where in your home would endospores most likely be found Wh b. .pdf
25. Radium is called a _________ and replaces calcium in bones and c.pdf
What skills does law enforcement need to combat cyber crimesSol.pdf
Using Microsoft Visual Basics (.NET) ONLY!(This assignment encompa.pdf
were CPA involved in Enron scandal act with integrity as defined by .pdf
16) What are the two types of current What is different about the fl.pdf
What are some weakness of the NERC standard CPS1 and CPS2.Please lis.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PDF
Computing-Curriculum for Schools in Ghana
PDF
RMMM.pdf make it easy to upload and study
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
202450812 BayCHI UCSC-SV 20250812 v17.pptx
GDM (1) (1).pptx small presentation for students
Abdominal Access Techniques with Prof. Dr. R K Mishra
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
Computing-Curriculum for Schools in Ghana
RMMM.pdf make it easy to upload and study
STATICS OF THE RIGID BODIES Hibbelers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

JAVAImplement the DFS algorithm. The implementation should be able.pdf

  • 1. JAVA Implement the DFS algorithm. The implementation should be able to find the shortest path from a source to any destination of a directed graph, print both the path and the cost of the path. Solution //updated code import java.util.Scanner; public class Dij { static Scanner scanner public static void main(String[] args) { int[] p = new int[5]; int minimum = 999, nextNode = 0; scanner = new Scanner(System.in); int[] distancematrix = new int[5]; int[][] actualmatrix = new int[5][5]; int[] visitedarray = new int[5]; System.out.println("Enter the cost matrix"); for (int i = 0; i < distancematrix.length; i++) { visitedarray[i] = 0; p[i] = 0; for (int j = 0; j < distancematrix.length; j++) { actualmatrix[i][j] = scanner.nextInt(); if (actualmatrix[i][j]==0) actualmatrix[i][j] = 999; } } distancematrix = actualmatrix[0]; visitedarray[0] = 1; distancematrix[0] = 0; for (int counter = 0; counter < 5; counter++)
  • 2. { min = 999; for (int i = 0; i < 5; i++) { if (minimum > distancematrix[i] && visitedarray[i]!=1) { minimum = distancematrix[i]; nextNode = i; } } visitedarray[nextNode] = 1; for (int i = 0; i < 5; i++) { if (visitedarray[i]!=1) { if (minimum+actualmatrix[nextNode][i] < distancematrix[i]) { distancematrix[i] =minimum+actualmatrix[nextNode][i]; p[i] = nextNode; } } } } for(int i = 0; i < 5; i++) System.out.print("|" + distancematrix[i]); System.out.println("|"); int j; for (int i = 0; i < 5; i++) { if (i!=0) { System.out.print("Path = " + i); j = i; do
  • 3. { j = p[j]; System.out.print(" <- " + j); } while(j != 0); } System.out.println(); } } }