School Commit Init
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package DirectedGraphJava;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static DirectedGraph read_from_file(String filename) throws FileNotFoundException {
|
||||
DirectedGraph graph = new DirectedGraph();
|
||||
File file = new File(filename);
|
||||
Scanner scanner = new Scanner(file);
|
||||
Integer num_vertices = scanner.nextInt();
|
||||
for(Integer i = 0; i < num_vertices; i++)
|
||||
graph.addVertex(i);
|
||||
Integer num_edges = scanner.nextInt();
|
||||
for(Integer i = 0; i < num_edges; i++){
|
||||
Integer v1 = scanner.nextInt();
|
||||
Integer v2 = scanner.nextInt();
|
||||
Integer w = scanner.nextInt();
|
||||
graph.addEdge(v1, v2, w);
|
||||
}
|
||||
scanner.close();
|
||||
return graph;
|
||||
}
|
||||
|
||||
public static void write_to_file(DirectedGraph graph, String filename) throws IOException {
|
||||
DirectedGraph new_graph = graph.reindex();
|
||||
File file = new File(filename);
|
||||
file.createNewFile();
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(new_graph.numVertices().toString() + " " + new_graph.numEdges().toString() + "\n");
|
||||
for(Iterator<Pair<Pair<Integer, Integer>,Integer>> edge = new_graph.edges(); edge.hasNext();){
|
||||
Pair<Pair<Integer, Integer>,Integer> e = edge.next();
|
||||
writer.write(e.first.first.toString() + " " + e.first.second.toString() + " " + e.second.toString() + "\n");
|
||||
}
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static DirectedGraph random_graph(Integer num_vertices, Integer num_edges) {
|
||||
DirectedGraph graph = new DirectedGraph();
|
||||
for(Integer i = 0; i < num_vertices; i++)
|
||||
graph.addVertex(i);
|
||||
for(Integer i = 0; i < num_edges; i++){
|
||||
Integer v1 = (int)(Math.random() * num_vertices);
|
||||
Integer v2 = (int)(Math.random() * num_vertices);
|
||||
Integer w = (int)(Math.random() * 100);
|
||||
if(graph.isEdge(v1, v2)){
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
graph.addEdge(v1, v2, w);
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ui UI = new ui();
|
||||
UI.run();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user