-
Notifications
You must be signed in to change notification settings - Fork 0
/
BipartiteGraph.java
116 lines (105 loc) · 3.43 KB
/
BipartiteGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package emp_proj;
import java.io.*;
import java.util.*;
public class BipartiteGraph
{
public static void main(String[] args) throws Exception
{
int n, m, maxCapacity, i, j, minCapacity;
double maxProbability, value, x;
System.out.println("\n\n---------------------------------------------------");
System.out.print("Enter number of nodes on the source side: \t");
n = GetInt();
System.out.print("Enter number of nodes on the sink side: \t");
m = GetInt();
System.out.print("Enter max probability: \t\t\t\t");
maxProbability = GetReal();
if(maxProbability > 1)
{
System.out.println("Max probability should be less than or equal to 1");
return;
}
System.out.print("Enter minimum capacity: \t\t\t");
minCapacity = GetInt();
System.out.print("Enter maximum capacity: \t\t\t");
maxCapacity = GetInt();
String directory = System.getProperty("user.dir");
System.out.print("Enter the output file name: \t\t\t");
String fileName = GetString() + ".txt";
System.out.println("---------------------------------------------------\n");
try
{
PrintWriter outFile = new PrintWriter(new FileWriter(new File(directory, fileName)));
//edges
double[][] edge = new double[n][m];
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
value = Math.random();
if(value <= maxProbability)
edge[i][j] = value;
else
edge[i][j] = 0;
}
}
System.out.println("-----------------------------------------");
System.out.println("\tSource\tSink\tCapacity");
System.out.println("-----------------------------------------");
//computing the edges out of source
for (i = 0; i < n; i++)
{
x=Math.random();
//Compute a capacity in range of [minCapacity, maxCapacity]
value = Math.floor(minCapacity + (x * (maxCapacity - minCapacity + 1)));
System.out.println("\t" + "s" + "\tl" + (i + 1) + "\t" + (int)value);
outFile.println("\t" + "s" + "\tl" + (i + 1) + "\t" + (int)value);
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(edge[i][j] > 0)
{
edge[i][j] = Math.floor(minCapacity + (edge[i][j] * (maxCapacity - minCapacity + 1)));
System.out.println("\tl"+ (i+1) + "\tr" + (j+1) + "\t" + (int)edge[i][j]);
//computing for the vertices between source and sink and writing them to the output file
outFile.println("\tl"+ (i+1) + "\tr" + (j+1) + "\t" + (int)edge[i][j]);
}
}
}
//computing the edges into the sink
for (j=0; j < m; j++)
{
x=Math.random();
value = Math.floor(minCapacity + (x * (maxCapacity - minCapacity + 1)));
System.out.println("\tr" + (j+1) + "\t" + "t" + "\t" + (int)value);
outFile.println("\tr" + (j + 1) + "\t" + "t" + "\t" + (int)value);
}
System.out.println("\n\nOutput is created at: \t" + directory + "\\" + fileName);
outFile.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
//helper functions
public static String GetString() throws IOException
{
BufferedReader stringIn = new BufferedReader (new
InputStreamReader(System.in));
return stringIn.readLine();
}
public static int GetInt() throws IOException
{
String aux = GetString();
return Integer.parseInt(aux);
}
public static double GetReal() throws IOException
{
String aux = GetString();
Double d = new Double(aux);
return d.doubleValue() ;
}
}