Skip to content

Commit 6b56145

Browse files
author
Bhrigu Kansra
authored
Merge branch 'master' into patch-1
2 parents 2643202 + acfcec7 commit 6b56145

File tree

8 files changed

+505
-4
lines changed

8 files changed

+505
-4
lines changed

Graph/Kruskal's Algorithm

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
class edge{
5+
public :
6+
int source;
7+
int destination;
8+
int weight;
9+
};
10+
11+
bool compare(edge e1, edge e2)
12+
{
13+
return e1.weight < e2.weight;
14+
}
15+
16+
int findParent(int v, int* parent)
17+
{
18+
if(parent[v] == v)
19+
return v;
20+
21+
return findParent(parent[v],parent);
22+
}
23+
24+
void kruskals(edge* input, int V, int E)
25+
{
26+
sort(input,input+E,compare);
27+
28+
edge* output = new edge[V-1];
29+
int *parent = new int[V];
30+
31+
for(int i = 0; i < V; i++)
32+
parent[i] = i;
33+
34+
int count = 0;
35+
int i = 0;
36+
37+
while(count < V-1)
38+
{
39+
int srcParent = findParent(input[i].source,parent);
40+
int destParent = findParent(input[i].destination,parent);
41+
if(srcParent != destParent)
42+
{
43+
output[count++] = input[i];
44+
parent[srcParent] = destParent;
45+
}
46+
i++;
47+
}
48+
49+
for(int i = 0; i < V-1; i++)
50+
{
51+
if(output[i].source < output[i].destination)
52+
cout<<output[i].source<<" "<<output[i].destination<<" "<<output[i].weight<<endl;
53+
else
54+
cout<<output[i].destination<<" "<<output[i].source<<" "<<output[i].weight<<endl;
55+
56+
}
57+
}
58+
59+
60+
int main()
61+
{
62+
int V, E,s,d,w;
63+
cin >> V >> E;
64+
/* Write Your Code Here
65+
Complete the Rest of the Program
66+
You have to Print the output yourself
67+
*/
68+
edge* input = new edge[E];
69+
for(int i = 0; i<E; i++)
70+
{
71+
cin>>s>>d>>w;
72+
input[i].source = s;
73+
input[i].destination = d;
74+
input[i].weight = w;
75+
}
76+
77+
78+
79+
kruskals(input,V,E);
80+
81+
return 0;
82+
}

Graph/Prim's Algorithm

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
int findMinVertex(int* weights, bool* visited, int V)
5+
{
6+
int minVertex = -1;
7+
for(int i = 0; i < V; i++)
8+
{
9+
if(visited[i] == false)
10+
{
11+
if(minVertex == -1 || weights[i] < weights[minVertex])
12+
minVertex = i;
13+
}
14+
}
15+
return minVertex;
16+
17+
}
18+
void prim(int** adjMatrix, int V)
19+
{
20+
bool* visited = new bool[V];
21+
int* parents = new int[V];
22+
int* weights = new int[V];
23+
24+
for(int i = 0; i < V; i++){
25+
weights[i] = INT_MAX;
26+
visited[i] = false;
27+
}
28+
29+
parents[0] = -1;
30+
weights[0] = 0;
31+
32+
for(int i = 0; i < V - 1; i++)
33+
{ //find minVertex
34+
int minVertex = findMinVertex(weights,visited,V);
35+
visited[minVertex] = true;
36+
//explore unvisited neighbours
37+
for(int j = 0; j < V; j++)
38+
{
39+
if(adjMatrix[minVertex][j] != 0 && !visited[j])
40+
{
41+
if(adjMatrix[minVertex][j] < weights[j])
42+
{
43+
weights[j] = adjMatrix[minVertex][j];
44+
parents[j] = minVertex;
45+
}
46+
}
47+
}
48+
}
49+
50+
51+
//printing
52+
for(int i = 1; i < V; i++)
53+
{
54+
if(parents[i] < i)
55+
cout << parents[i] << " " << i << " " << weights[i] << endl;
56+
else
57+
cout << i << " " << parents[i] << " " << weights[i] << endl;
58+
59+
}
60+
61+
62+
63+
64+
}
65+
int main()
66+
{
67+
int V, E, tempX, tempY;
68+
cin >> V >> E;
69+
70+
/*
71+
72+
Write Your Code Here
73+
Complete the Rest of the Program
74+
You have to Print the output yourself
75+
76+
*/
77+
int ** adjMatrix = new int*[V];
78+
for(int i = 0; i < V; i++)
79+
{
80+
adjMatrix[i] = new int[V];
81+
for(int j = 0; j < V; j++)
82+
adjMatrix[i][j] = 0;
83+
}
84+
85+
86+
for(int i = 0; i<E; i++)
87+
{int s,d,w;
88+
cin>>s>>d>>w;
89+
adjMatrix[s][d] = w;
90+
adjMatrix[d][s] = w;
91+
}
92+
93+
prim(adjMatrix,V);
94+
95+
for(int i = 0; i < V; i++)
96+
delete [] adjMatrix[i];
97+
98+
delete [] adjMatrix;
99+
100+
return 0;
101+
}

Prim's MST algorithm

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
#include <stdio.h>
3+
#include <limits.h>
4+
#include<stdbool.h>
5+
6+
#define V 5
7+
8+
int minKey(int key[], bool mstSet[])
9+
{
10+
11+
int min = INT_MAX, min_index;
12+
13+
for (int v = 0; v < V; v++)
14+
if (mstSet[v] == false && key[v] < min)
15+
min = key[v], min_index = v;
16+
17+
return min_index;
18+
}
19+
20+
21+
int printMST(int parent[], int n, int graph[V][V])
22+
{
23+
printf("Edge \tWeight\n");
24+
for (int i = 1; i < V; i++)
25+
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
26+
}
27+
28+
void primMST(int graph[V][V])
29+
{
30+
31+
int parent[V];
32+
33+
int key[V];
34+
35+
bool mstSet[V];
36+
37+
for (int i = 0; i < V; i++)
38+
key[i] = INT_MAX, mstSet[i] = false;
39+
40+
41+
key[0] = 0;
42+
parent[0] = -1; // First node is always root of MST
43+
44+
45+
for (int count = 0; count < V-1; count++)
46+
{
47+
48+
int u = minKey(key, mstSet);
49+
50+
51+
mstSet[u] = true;
52+
53+
for (int v = 0; v < V; v++)
54+
55+
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
56+
parent[v] = u, key[v] = graph[u][v];
57+
}
58+
59+
60+
printMST(parent, V, graph);
61+
}
62+
63+
64+
65+
int main()
66+
{
67+
/* Let us create the following graph
68+
2 3
69+
(0)--(1)--(2)
70+
| / \ |
71+
6| 8/ \5 |7
72+
| / \ |
73+
(3)-------(4)
74+
9 */
75+
int graph[V][V] = {{0, 2, 0, 6, 0},
76+
{2, 0, 3, 8, 5},
77+
{0, 3, 0, 0, 7},
78+
{6, 8, 0, 0, 9},
79+
{0, 5, 7, 9, 0}};
80+
81+
82+
primMST(graph);
83+
84+
return 0;
85+
}

Tower Of Hanoi/Tower_Of_Hanoi.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
public class Tower_Of_Hanoi {
4+
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int nod = sc.nextInt();
8+
int count = towerOfHanoiSteps("Ring on 1", "Ring on 2", "Ring on 3", nod,0);
9+
System.out.println("No. of Steps : " + count);
10+
sc.close();
11+
}
12+
13+
private static int towerOfHanoiSteps(String p1, String p2, String p3, int no_of_discs,int count) {
14+
if (no_of_discs == 1) {
15+
System.out.println(p1 + " goes to " + p3);
16+
count++;
17+
} else {
18+
count = towerOfHanoiSteps(p1, p3, p2, no_of_discs - 1,count);
19+
System.out.println(p1 + " goes to " + p3);
20+
count++;
21+
count = towerOfHanoiSteps(p2, p1, p3, no_of_discs - 1,count);
22+
}
23+
return count;
24+
}
25+
}

0 commit comments

Comments
 (0)