Skip to content

Commit

Permalink
Add codes for algorithms which were coded in university year.
Browse files Browse the repository at this point in the history
  • Loading branch information
MosabbirShiblu committed Apr 18, 2017
0 parents commit f54b6d7
Show file tree
Hide file tree
Showing 40 changed files with 3,433 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>UniversityAlgorithm</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
8 changes: 8 additions & 0 deletions Gauss.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
3
3 6 1
2 4 3
1 3 2

16
13
9
14 changes: 14 additions & 0 deletions InputPrims.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
10 13
1 2 32
1 4 17
2 5 45
4 5 10
3 4 18
5 6 28
3 7 5
7 8 59
4 8 3
8 9 4
5 9 25
9 10 12
6 10 6
19 changes: 19 additions & 0 deletions InputPrims2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
6 13
1 3 72
1 4 50
1 5 90
1 6 35

2 3 71
2 4 70
2 5 73
2 6 75

3 5 77
3 6 90

4 5 60
4 6 40

5 6 80

11 changes: 11 additions & 0 deletions MSTUnchanged.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
7 8
1 5 5
1 6 8
3 5 1
3 6 1
2 6 1
2 7 3
4 6 1
4 7 2

1 5
141 changes: 141 additions & 0 deletions Prims.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package algorithm;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Prims {

int [][]cost;
int node,k,l;
final int INFINITY=Integer.MAX_VALUE;
int minCost=INFINITY;

int t[][];//answer array

void go(){

node=7;//no of nodes
//takeInput();
takeInputFromFile("InputPrims.txt");
System.out.println("weight: " + prims());
printAnswer();
}

void takeInputFromFile(String fileName){
int edge;
try {
Scanner sc=new Scanner(new FileReader(fileName));

node=sc.nextInt();//node number
edge=sc.nextInt();//edge number

//now initialize costs
cost=new int[node+1][node+1]; //index starts from `1

for(int i=1;i<=node;i++)
for(int j=1;j<=node;j++)
cost[i][j]=INFINITY;

for(int i=1;i<=node;i++)
cost[i][i]=0; //own cost is 0

//now read from file
int u,v,w;//first vertex,2nd vertex,cost/wieght
for(int i=1;i<=edge;i++){
u=sc.nextInt();//read the next int in the input file
v=sc.nextInt();
w=sc.nextInt();



cost[u][v]=w;
cost[v][u]=w;//undirected graph so viec versa connected

if(w<minCost)
{
minCost=cost[u][v];
k=u;
l=v; //find these by looping while taking input

}

}//for


} catch (FileNotFoundException e) {
e.printStackTrace();
}
}//input


int prims(){

t=new int[node][3];

minCost=cost[k][l];

t[1][1]=k; //1 to 6 k=1
t[1][2]=l; //l=6

int i,j,minIndex=-1;

int near[]=new int[node+1];//keep tracks of the minimum cost vertices from the index vertex

//create the near array
for( i=1;i<=node;i++)

if(cost[i][l]<cost[i][k]) //cheking if the edges from 1 and 6 which is minimum to their nearest
near[i]=l;//that means edge from 6 is less than theedge from 1
else
near[i]=k;

//end for

near[k]=0;
near[l]=0; //we have already found near of 1 and 6

for(i=2;i<node;i++){

//find the lowest cost vertex
int minimumCost=INFINITY;

for( j=1;j<=node;j++){

if(near[j]!=0 && cost[j][near[j]]<minimumCost){ //find min cost index

minimumCost=cost[j][near[j]];//new minmum
minIndex=j;//save it in another variable beacuse j will be always n after this loop

}//if


}//for
j=minIndex;

t[i][1]=j;
t[i][2]=near[j];

minCost=minCost+cost[j][near[j]];
near[j]=0;


//update the near array
for(int k=1;k<=node;k++)

if(near[k]!=0 && cost[k][j]<cost[k][near[k]])
near[k]=j;
}//for
return minCost;
}//prim

void printAnswer(){

System.out.println("Edges in the MST:");
for(int i=1;i<=node-1;i++)
System.out.println(t[i][1]+" "+t[i][2]);
}
public static void main(String []args){
new Prims().go();
}
}
Binary file added Prims.rar
Binary file not shown.
8 changes: 8 additions & 0 deletions SCC.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
4
6
1 1
2 2
3 3
4 4
1 2
2 3
Loading

0 comments on commit f54b6d7

Please sign in to comment.