0% found this document useful (0 votes)
18 views6 pages

Graph Theory Noman Nadeem Assignment01

The document describes an implementation of Dijkstra's algorithm in Java to find the shortest path between nodes in a graph using an adjacency matrix. It includes code to build a graph from user input, run Dijkstra's algorithm on the graph from a specified start node, and output the shortest distances.

Uploaded by

Noman kiani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Graph Theory Noman Nadeem Assignment01

The document describes an implementation of Dijkstra's algorithm in Java to find the shortest path between nodes in a graph using an adjacency matrix. It includes code to build a graph from user input, run Dijkstra's algorithm on the graph from a specified start node, and output the shortest distances.

Uploaded by

Noman kiani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Noman Nadeem

Sap id: 19690


Subject: Graph Theory
Instructor: Sir Mubariz Rehman

Assignment 1
Dijkstra Algorithm Implementation in Java Using Adjacency Matrix
Code
import java.util.*;

class DijkstraAlgorithm {
private int vertices;
private List<Edge>[] adjList;

public DijkstraAlgorithm(int vertices) {


this.vertices = vertices;
adjList = new ArrayList[vertices];
for (int i = 0; i < vertices; i++) {
adjList[i] = new ArrayList<>();
}
}

public void addEdge(int source, int destination, int weight) {


adjList[source].add(new Edge(destination, weight));
adjList[destination].add(new Edge(source, weight)); // For undirected
graph
}

public void dijkstra(int startVertex) {


int[] distance = new int[vertices];
boolean[] visited = new boolean[vertices];
Arrays.fill(distance, Integer.MAX_VALUE);

distance[startVertex] = 0;
for (int i = 0; i < vertices - 1; i++) {
int minVertex = findMinVertex(distance, visited);
visited[minVertex] = true;

for (Edge edge : adjList[minVertex]) {


if (!visited[edge.destination] && distance[minVertex] !=
Integer.MAX_VALUE) {
int newDist = distance[minVertex] + edge.weight;
if (newDist < distance[edge.destination]) {
distance[edge.destination] = newDist;
}
}
}
}

// Print the shortest distances


for (int i = 0; i < vertices; i++) {
System.out.println("Vertex " + i + ": Distance = " + distance[i]);
}
}

private int findMinVertex(int[] distance, boolean[] visited) {


int minVertex = -1;
for (int i = 0; i < vertices; i++) {
if (!visited[i] && (minVertex == -1 || distance[i] < distance[minVertex]))
{
minVertex = i;
}
}
return minVertex;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of vertices: ");


int vertices = scanner.nextInt();

DijkstraAlgorithm graph = new DijkstraAlgorithm(vertices);

System.out.print("Enter the number of edges: ");


int edges = scanner.nextInt();

for (int i = 0; i < edges; i++) {


System.out.print("Enter source, destination, and weight of edge " + (i +
1) + ": ");
int source = scanner.nextInt();
int destination = scanner.nextInt();
int weight = scanner.nextInt();
graph.addEdge(source, destination, weight);
}

System.out.print("Enter the start vertex: ");


int startVertex = scanner.nextInt();

graph.dijkstra(startVertex);
scanner.close();
}
}

class Edge {
int destination;
int weight;

public Edge(int destination, int weight) {


this.destination = destination;
this.weight = weight;
}
}

You might also like