Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e353a55

Browse files
lab Work Added
0 parents commit e353a55

File tree

15 files changed

+1444
-0
lines changed

15 files changed

+1444
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import javafx.util.Pair;
2+
import java.util.Comparator;
3+
import java.util.LinkedList;
4+
import java.util.PriorityQueue;
5+
import java.util.Scanner;
6+
7+
public class DijkstraAdjacencyList {
8+
static class Graph {
9+
int vertices;
10+
LinkedList<Edge>[] adjacencylist;
11+
12+
Graph(int vertices) {
13+
this.vertices = vertices;
14+
adjacencylist = new LinkedList[vertices];
15+
for (int i = 0; i < vertices; i++) {
16+
adjacencylist[i] = new LinkedList<>();
17+
}
18+
}
19+
20+
public void addEdge(int source, int destination, int weight) {
21+
Edge edge = new Edge(source, destination, weight);
22+
adjacencylist[source].addFirst(edge);
23+
edge = new Edge(destination, source, weight);
24+
adjacencylist[destination].addFirst(edge); // for undirected graph
25+
}
26+
27+
public void dijkstra_GetMinDistances(int sourceVertex) {
28+
boolean[] SPT = new boolean[vertices];
29+
int[] distance = new int[vertices];
30+
for (int i = 0; i < vertices; i++) {
31+
distance[i] = Integer.MAX_VALUE;
32+
}
33+
PriorityQueue<Pair<Integer, Integer>> pq = new PriorityQueue<>(vertices,
34+
new Comparator<Pair<Integer, Integer>>() {
35+
@Override
36+
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
37+
// sort using distance values
38+
int key1 = p1.getKey();
39+
int key2 = p2.getKey();
40+
return key1 - key2;
41+
}
42+
});
43+
distance[0] = 0;
44+
Pair<Integer, Integer> p0 = new Pair<>(distance[0], 0);
45+
pq.offer(p0);
46+
while (!pq.isEmpty()) {
47+
Pair<Integer, Integer> extractedPair = pq.poll();
48+
int extractedVertex = extractedPair.getValue();
49+
if (SPT[extractedVertex] == false) {
50+
SPT[extractedVertex] = true;
51+
LinkedList<Edge> list = adjacencylist[extractedVertex];
52+
for (int i = 0; i < list.size(); i++) {
53+
Edge edge = list.get(i);
54+
int destination = edge.destination;
55+
// only if edge destination is not present in mst
56+
if (SPT[destination] == false) {
57+
int newKey = distance[extractedVertex] + edge.weight;
58+
int currentKey = distance[destination];
59+
if (currentKey > newKey) {
60+
Pair<Integer, Integer> p = new Pair<>(newKey, destination);
61+
pq.offer(p);
62+
distance[destination] = newKey;
63+
}
64+
}
65+
}
66+
}
67+
}
68+
printDijkstra(distance, sourceVertex);
69+
}
70+
71+
public void printDijkstra(int[] distance, int sourceVertex) {
72+
System.out.println("Dijkstra Algorithm: (Adjacency List + Priority Queue)");
73+
for (int i = 0; i < vertices; i++) {
74+
System.out.println("Source Vertex: " + sourceVertex + " to vertex " + +i + " distance: " + distance[i]);
75+
}
76+
}
77+
}
78+
79+
static class Edge {
80+
int source;
81+
int destination;
82+
int weight;
83+
84+
public Edge(int source, int destination, int weight) {
85+
this.source = source;
86+
this.destination = destination;
87+
this.weight = weight;
88+
}
89+
}
90+
91+
public static void main(String[] args) {
92+
Scanner sc = new Scanner(System.in);
93+
System.out.println("Enter the number of vertices");
94+
int v = sc.nextInt();
95+
Graph graph = new Graph(v);
96+
System.out.println("Enter the number of edge");
97+
int e = sc.nextInt();
98+
for (int i = 0; i < e; i++) {
99+
System.out.println("Enter Source for " + (i + 1));
100+
int s = sc.nextInt();
101+
System.out.println("Enter Destination for " + (i + 1));
102+
int d = sc.nextInt();
103+
System.out.println("Enter Weight for " + (i + 1));
104+
int w = sc.nextInt();
105+
graph.addEdge(s, d, w);
106+
}
107+
System.out.println("Enter the source vertex");
108+
int sv = sc.nextInt();
109+
graph.dijkstra_GetMinDistances(sv);
110+
System.out.println("IshanGupta-19BCE7467");
111+
}
112+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.Scanner;
2+
3+
public class DijkstraAdjacencyMatrix {
4+
static class Graph {
5+
int vertices;
6+
int matrix[][];
7+
8+
public Graph(int vertex) {
9+
this.vertices = vertex;
10+
matrix = new int[vertex][vertex];
11+
}
12+
13+
public void addEdge(int source, int destination, int weight) {
14+
matrix[source][destination] = weight;
15+
matrix[destination][source] = weight;
16+
}
17+
18+
int getMinimumVertex(boolean[] mst, int[] key) {
19+
int minKey = Integer.MAX_VALUE;
20+
int vertex = (-1);
21+
for (int i = 0; i < vertices; i++) {
22+
if (!mst[i] && minKey > key[i]) {
23+
minKey = key[i];
24+
vertex = i;
25+
}
26+
}
27+
return vertex;
28+
}
29+
30+
public void dijkstra_GetMinDistances(int sourceVertex) {
31+
boolean[] spt = new boolean[vertices];
32+
int[] distance = new int[vertices];
33+
int INFINITY = Integer.MAX_VALUE;
34+
for (int i = 0; i < vertices; i++) {
35+
distance[i] = INFINITY;
36+
}
37+
distance[sourceVertex] = 0;
38+
for (int i = 0; i < vertices; i++) {
39+
int vertex_U = getMinimumVertex(spt, distance);
40+
spt[vertex_U] = true;
41+
for (int vertex_V = 0; vertex_V < vertices; vertex_V++) {
42+
if (matrix[vertex_U][vertex_V] > 0) {
43+
if (!spt[vertex_V] && matrix[vertex_U][vertex_V] != INFINITY) {
44+
int newKey = matrix[vertex_U][vertex_V] + distance[vertex_U];
45+
if (newKey < distance[vertex_V])
46+
distance[vertex_V] = newKey;
47+
}
48+
}
49+
}
50+
}
51+
printDijkstra(sourceVertex, distance);
52+
}
53+
54+
public void printDijkstra(int sourceVertex, int[] key) {
55+
System.out.println("Dijkstra Algorithm: (Adjacency Matrix)");
56+
for (int i = 0; i < vertices; i++) {
57+
System.out.println("Source Vertex: " + sourceVertex + " to vertex " + i + " distance: " + key[i]);
58+
}
59+
}
60+
}
61+
62+
public static void main(String[] args) {
63+
Scanner sc = new Scanner(System.in);
64+
System.out.println("Enter the number of vertices");
65+
int v = sc.nextInt();
66+
Graph graph = new Graph(v);
67+
System.out.println("Enter the number of edge");
68+
int e = sc.nextInt();
69+
for (int i = 0; i < e; i++) {
70+
System.out.println("Enter Source for " + (i + 1));
71+
int s = sc.nextInt();
72+
System.out.println("Enter Destination for " + (i + 1));
73+
int d = sc.nextInt();
74+
System.out.println("Enter Weight for " + (i + 1));
75+
int w = sc.nextInt();
76+
graph.addEdge(s, d, w);
77+
}
78+
System.out.println("Enter the source vertex:");
79+
int sv = sc.nextInt();
80+
graph.dijkstra_GetMinDistances(sv);
81+
System.out.println("IshanGupta-19BCE7467");
82+
}
83+
}

‎Kruskal Algorithm/KruskalMST.java‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.ishan.daa;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
class KruskalMST {
7+
class Edge implements Comparable<Edge>
8+
{
9+
int src, dest, weight;
10+
public int compareTo(Edge compareEdge)
11+
{
12+
return this.weight - compareEdge.weight;
13+
}
14+
};
15+
class subset
16+
{
17+
int parent, rank;
18+
};
19+
int V, E; // V-> no. of vertices & E->no.of edges
20+
Edge edge[]; // collection of all edges
21+
KruskalMST(int v, int e)
22+
{
23+
V = v;
24+
E = e;
25+
edge = new Edge[E];
26+
for (int i = 0; i < e; ++i)
27+
edge[i] = new Edge();
28+
}
29+
int find(subset subsets[], int i)
30+
{
31+
if (subsets[i].parent != i)
32+
subsets[i].parent = find(subsets, subsets[i].parent);
33+
return subsets[i].parent;
34+
}
35+
void Union(subset subsets[], int x, int y)
36+
{
37+
int xroot = find(subsets, x);
38+
int yroot = find(subsets, y);
39+
if (subsets[xroot].rank
40+
< subsets[yroot].rank)
41+
subsets[xroot].parent = yroot;
42+
else if (subsets[xroot].rank
43+
> subsets[yroot].rank)
44+
subsets[yroot].parent = xroot;
45+
else {
46+
subsets[yroot].parent = xroot;
47+
subsets[xroot].rank++;
48+
}
49+
}
50+
void Kruskal()
51+
{
52+
Edge result[] = new Edge[V];
53+
int e = 0;
54+
int i = 0;
55+
for (i = 0; i < V; ++i)
56+
result[i] = new Edge();
57+
Arrays.sort(edge);
58+
subset subsets[] = new subset[V];
59+
for (i = 0; i < V; ++i)
60+
subsets[i] = new subset();
61+
for (int v = 0; v < V; ++v)
62+
{
63+
subsets[v].parent = v;
64+
subsets[v].rank = 0;
65+
}
66+
i = 0; // Index used to pick next edge
67+
while (e < V - 1)
68+
{
69+
Edge next_edge = edge[i++];
70+
int x = find(subsets, next_edge.src);
71+
int y = find(subsets, next_edge.dest);
72+
if (x != y) {
73+
result[e++] = next_edge;
74+
Union(subsets, x, y);
75+
}
76+
}
77+
System.out.println("Following are the edges in " + "the constructed MST");
78+
int minimumCost = 0;
79+
for (i = 0; i < e; ++i)
80+
{
81+
System.out.println(result[i].src + " -- "
82+
+ result[i].dest
83+
+ " == " + result[i].weight);
84+
minimumCost += result[i].weight;
85+
}
86+
System.out.println("Minimum Cost Spanning Tree "
87+
+ minimumCost);
88+
}
89+
public static void main(String[] args)
90+
{
91+
Scanner sc = new Scanner(System.in);
92+
System.out.println("Enter the number of vertices");
93+
int v = sc.nextInt();
94+
System.out.println("Enter the number of edge");
95+
int e = sc.nextInt();
96+
KruskalMST graph = new KruskalMST(v, e);
97+
for (int i=0;i<e;i++){
98+
System.out.println("Enter Source for "+(i+1));
99+
int s=sc.nextInt();
100+
System.out.println("Enter Destination for "+(i+1));
101+
int d=sc.nextInt();
102+
System.out.println("Enter Weight for "+(i+1));
103+
int w=sc.nextInt();
104+
graph.edge[i].src = s;
105+
graph.edge[i].dest = d;
106+
graph.edge[i].weight = w;
107+
}
108+
System.out.println("19BCE7467-Ishan Gupta");
109+
graph.Kruskal();
110+
}}
111+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /