/
EdgeListToJsonConversionFormatClassSorted.java
187 lines (146 loc) · 3.12 KB
/
EdgeListToJsonConversionFormatClassSorted.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
@purpose
This algorithm works with Undirected Graphs -> i.e for every edge in the graph
it is assumed that it is a directed edge in both the directions
@author
Aditya and Syed
@job
Convert an edge list based file to JSON Converted File based on the relative sorted ordering
@input
Simple Edge list format
@output
Dual Edge List Format
JSON Converted Format with relative sorted ordering
*/
import java.util.Scanner;
public class EdgeListToJsonConversionFormatClassSorted{
public static void convert(int[] a, int[] b, int n){
//int vertex_count = 0;
int val = 0;
int j;
for(int i=0;i<n;i++){
val = a[i];
System.out.print("["+a[i]+",0,[["+b[i]+",1]");
j=i+1;
//vertex_count++;
while(j<n && a[j] == val){
System.out.print(",["+b[j]+",1]");
j++;
}
System.out.println("]]");
i = j-1;
}
//System.out.println("Number of vertices "+vertex_count);
//System.out.println("Number of edges "+edge_count);
}
public static void sort_relative_second(int[] a, int[] b, int n)
{
int i, val, j;
i=0;
while(i<n)
{
val = a[i];
j = i+1;
while(j<=n-1 && a[j]==val)
{
j++;
}
for(int c1=i;c1<j-1;c1++)
{
int temp_key = c1;
for(int c2=c1+1;c2<j;c2++)
{
if(b[c2]<b[temp_key])
temp_key = c2;
}
int temp = b[temp_key];
b[temp_key] = b[c1];
b[c1] = temp;
}
i = j;
}
//display2(a, b, n);
convert(a, b, n);
}
public static void display2(int[] a, int[] b, int n)
{
for(int i=0;i<n;i++)
System.out.println(a[i]+" "+b[i]);
System.out.println();
}
public static void display(int[] a, int n)
{
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int count = 0;
/**
Calculate the count of the number of vertices pair in the graph
*/
for(String s : args){
count++;
}
/**
Declaring the arrays to hold the input
*/
int[] arr = new int[count];
int[] num1 = new int[count/2];
int[] num2 = new int[count/2];
int parity = 0, k1=0, k2=0;
for(String s : args){
arr[parity] = Integer.parseInt(s);
if(parity%2==0)
num1[k1++] = Integer.parseInt(s);
else
num2[k2++] = Integer.parseInt(s);
parity++;
}
//display(arr, arr.length);
//display(num1, num1.length);
//display(num2, num2.length);
//display2(num1, num2, num1.length);
/**
Converting the arrays into the Dual Edge Lis Based Format
*/
int[] a = new int[count];
int[] b = new int[count];
int i;
for(i=0;i<num1.length;i++)
{
a[i] = num1[i];
b[i] = num2[i];
}
for(int k=0;k<num2.length;k++)
{
a[k+i] = num2[k];
b[k+i] = num1[k];
}
//display2(a, b, a.length);
/**
Sorting the array relative to the first array
*/
for(i=0;i<a.length-1; i++)
{
int key = i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[key])
key = j;
}
int temp1 = a[key];
int temp2 = b[key];
a[key] = a[i];
b[key] = b[i];
a[i] = temp1;
b[i] = temp2;
}
//display2(a, b, a.length);
/**
Provide even more sorting to sort the indices of the second array if the index of the first array is the same
*/
sort_relative_second(a, b, a.length);
}
}