SlideShare a Scribd company logo
Program:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;
// A Java program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
import java.util.*;
import java.lang.*;
import java.io.*;
class DijkstraAlgorithem
{
// A utility function to find the vertex with minimum distance value,
// from the set of vertices not yet included in shortest path tree
static final int V=6;
int minDistance(int dist[], Boolean sptSet[])
{
// Initialize min value
int min = Integer.MAX_VALUE, min_index=-1;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
{
min = dist[v];
min_index = v;
}
return min_index;
}
// A utility function to print the constructed distance array
void print
Solution
(int dist[], int n,int src)
{
System.out.println( " Here Infinite="+Integer.MAX_VALUE);
System.out.println(" Source Vertex:"+(src+1));
System.out.println("Vertex Distance from Source");
for (int i = 0; i < V; i++)
System.out.println((i+1)+" tt "+dist[i]);
}
// Funtion that implements Dijkstra's single source shortest path
// algorithm for a graph represented using adjacency matrix
// representation
void dijkstra(int graph[][], int src)
{
int dist[] = new int[V]; // The output array. dist[i] will hold
// the shortest distance from src to i
// sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
Boolean sptSet[] = new Boolean[V];
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
{
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V-1; count++)
{
// Pick the minimum distance vertex from the set of vertices
// not yet processed. u is always equal to src in first
// iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the
// picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an
// edge from u to v, and total weight of path from src to
// v through u is smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v]!=0 &&
dist[u] != Integer.MAX_VALUE &&
dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
print

More Related Content

PDF
#include #include Number of vertices in the graph #define V 9 .pdf
PPTX
15-bellmanFord.pptx...........................................
PPT
9_graphs2.v4.ppt
PDF
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
PPT
Dijkstra's algorithm for computer science
PPT
Dijkstra algorithm ds 57612334t4t44.ppt
PPT
Dijkstra Shortest Path Algorithm in Network.ppt
PPT
Dijesktra 1.ppt
#include #include Number of vertices in the graph #define V 9 .pdf
15-bellmanFord.pptx...........................................
9_graphs2.v4.ppt
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Dijkstra's algorithm for computer science
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
Dijesktra 1.ppt

Similar to Program To change this license header, choose License Heade.pdf (20)

DOCX
Shortest Path Problem.docx
PPTX
Dijkstra’s algorithm
PDF
All pairs shortest path algorithm
PPT
2.3 shortest path dijkstra’s
PPTX
Graph Algorithms
PPTX
DIJKSTRA_123.pptx
PPT
Shortest path
PPTX
Dijkstra's algorithm presentation
PDF
Daa chpater14
PDF
Dijkstra's shortest path algorithm
PPT
Unit26 shortest pathalgorithm
PDF
White Grey Minimalist Geometric Project Presentation.pdf
PPTX
Data structure and algorithm
PPTX
computer networks lab program Bellman Ford.pptx
PPT
Lec-35Graph - Graph - Copy in Data Structure
PPTX
Session 13 - Single Source Shortest Path Method.pptx
PDF
Chap10 slides
PDF
CPSC125 ch6 sec3
PDF
Cpsc125 ch6sec3
PPTX
Randomized algorithms all pairs shortest path
Shortest Path Problem.docx
Dijkstra’s algorithm
All pairs shortest path algorithm
2.3 shortest path dijkstra’s
Graph Algorithms
DIJKSTRA_123.pptx
Shortest path
Dijkstra's algorithm presentation
Daa chpater14
Dijkstra's shortest path algorithm
Unit26 shortest pathalgorithm
White Grey Minimalist Geometric Project Presentation.pdf
Data structure and algorithm
computer networks lab program Bellman Ford.pptx
Lec-35Graph - Graph - Copy in Data Structure
Session 13 - Single Source Shortest Path Method.pptx
Chap10 slides
CPSC125 ch6 sec3
Cpsc125 ch6sec3
Randomized algorithms all pairs shortest path

More from annaelctronics (20)

PDF
two singals are expected. one is for CH3, the oth.pdf
PDF
There is in fact a stronger problem, namely 2p C .pdf
PDF
The term Lewis acid refers to a definition of aci.pdf
PDF
NHC(=O)CH3 NH2 OH .pdf
PDF
MnO2 may act as a catalyst.in the other reaction .pdf
PDF
What are P and R hereSolutionWhat are P and R here.pdf
PDF
Intermolecular attractions are attractions betwee.pdf
PDF
It is insoluble in water. .pdf
PDF
Write it using the ^ symbol. For example 1.34 10^6Solutio.pdf
PDF
Xenodiagnosis is the method used to document presesnce of a microorg.pdf
PDF
u(x)= ( -4x)Solutionu(x)= ( -4x).pdf
PDF
There are many more conditions than just the norms of the culture th.pdf
PDF
The three layers of smooth muscle that includes mucosa. innermost tu.pdf
PDF
The independent variable is the one which changes in each plant. Thi.pdf
PDF
Species diversity is more in near island as rate of immigration is m.pdf
PDF
Solution Flies do not have teeth for chewing food.So,like human f.pdf
PDF
plexusFormed from anterioe rani o these spinal nervesMajor nerve.pdf
PDF
Ok, so the number of double bond equivalents in the compound is [C4H.pdf
PDF
order of overlap of atomic orbitals from highest extent of overlap t.pdf
PDF
Na3NSolutionNa3N.pdf
two singals are expected. one is for CH3, the oth.pdf
There is in fact a stronger problem, namely 2p C .pdf
The term Lewis acid refers to a definition of aci.pdf
NHC(=O)CH3 NH2 OH .pdf
MnO2 may act as a catalyst.in the other reaction .pdf
What are P and R hereSolutionWhat are P and R here.pdf
Intermolecular attractions are attractions betwee.pdf
It is insoluble in water. .pdf
Write it using the ^ symbol. For example 1.34 10^6Solutio.pdf
Xenodiagnosis is the method used to document presesnce of a microorg.pdf
u(x)= ( -4x)Solutionu(x)= ( -4x).pdf
There are many more conditions than just the norms of the culture th.pdf
The three layers of smooth muscle that includes mucosa. innermost tu.pdf
The independent variable is the one which changes in each plant. Thi.pdf
Species diversity is more in near island as rate of immigration is m.pdf
Solution Flies do not have teeth for chewing food.So,like human f.pdf
plexusFormed from anterioe rani o these spinal nervesMajor nerve.pdf
Ok, so the number of double bond equivalents in the compound is [C4H.pdf
order of overlap of atomic orbitals from highest extent of overlap t.pdf
Na3NSolutionNa3N.pdf

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Lesson notes of climatology university.
PPTX
Presentation on HIE in infants and its manifestations
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
Lesson notes of climatology university.
Presentation on HIE in infants and its manifestations
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O7-L3 Supply Chain Operations - ICLT Program

Program To change this license header, choose License Heade.pdf

  • 1. Program: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chegg; // A Java program for Dijkstra's single source shortest path algorithm. // The program is for adjacency matrix representation of the graph import java.util.*; import java.lang.*; import java.io.*; class DijkstraAlgorithem { // A utility function to find the vertex with minimum distance value, // from the set of vertices not yet included in shortest path tree static final int V=6; int minDistance(int dist[], Boolean sptSet[]) { // Initialize min value int min = Integer.MAX_VALUE, min_index=-1; for (int v = 0; v < V; v++) if (sptSet[v] == false && dist[v] <= min) { min = dist[v]; min_index = v; } return min_index; } // A utility function to print the constructed distance array void print
  • 2. Solution (int dist[], int n,int src) { System.out.println( " Here Infinite="+Integer.MAX_VALUE); System.out.println(" Source Vertex:"+(src+1)); System.out.println("Vertex Distance from Source"); for (int i = 0; i < V; i++) System.out.println((i+1)+" tt "+dist[i]); } // Funtion that implements Dijkstra's single source shortest path // algorithm for a graph represented using adjacency matrix // representation void dijkstra(int graph[][], int src) { int dist[] = new int[V]; // The output array. dist[i] will hold // the shortest distance from src to i // sptSet[i] will true if vertex i is included in shortest // path tree or shortest distance from src to i is finalized Boolean sptSet[] = new Boolean[V]; // Initialize all distances as INFINITE and stpSet[] as false for (int i = 0; i < V; i++) { dist[i] = Integer.MAX_VALUE; sptSet[i] = false; } // Distance of source vertex from itself is always 0 dist[src] = 0; // Find shortest path for all vertices for (int count = 0; count < V-1; count++) { // Pick the minimum distance vertex from the set of vertices
  • 3. // not yet processed. u is always equal to src in first // iteration. int u = minDistance(dist, sptSet); // Mark the picked vertex as processed sptSet[u] = true; // Update dist value of the adjacent vertices of the // picked vertex. for (int v = 0; v < V; v++) // Update dist[v] only if is not in sptSet, there is an // edge from u to v, and total weight of path from src to // v through u is smaller than current value of dist[v] if (!sptSet[v] && graph[u][v]!=0 && dist[u] != Integer.MAX_VALUE && dist[u]+graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; } // print the constructed distance array print