-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBridgeNodeGraph.java
More file actions
executable file
·103 lines (82 loc) · 3.09 KB
/
BridgeNodeGraph.java
File metadata and controls
executable file
·103 lines (82 loc) · 3.09 KB
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
/**
* Author: Kevin Richardson <[email protected]>
* Date: 2011-Dec-7
* Time: 9:00 AM
*
* Contains generic methods for operating upon a graph's table.
* This class utilizes an array list of BridgeNodes to store its connections
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class BridgeNodeGraph
{
// This array list will contain an easy way to reference individual nodes as we build the graph.
ArrayList<BridgeNode> connections;
// Used by the constructor to read in the graph's information file.
Scanner fileScanner;
// Used to store an examined file line and its components.
String line;
String[] pieces;
// Constructs a graph out of the information in inputFile.
BridgeNodeGraph(String inputFile)
{
try
{
fileScanner = new Scanner(new File(inputFile));
}
catch(FileNotFoundException e)
{
System.out.println("The specified file was not found.");
System.exit(-1);
}
// Get the number of nodes and edges from the first line.
line = fileScanner.nextLine();
pieces = line.split(" ");
int numNodes = Integer.parseInt(pieces[0]);
int numEdges = Integer.parseInt(pieces[1]);
// Create an array list to store nodes.
connections = new ArrayList<BridgeNode>();
// Create an empty node in the list for each node in the graph.
for(int i = 0; i < numNodes; i++)
{
connections.add(i, new BridgeNode(i));
}
// Run through the connections and connect nodes where appropriate.
for(int i = 0; i < numEdges; i++)
{
line = fileScanner.nextLine();
pieces = line.split(" ");
int origin = Integer.parseInt(pieces[0]);
int destination = Integer.parseInt(pieces[1]);
// Get the origin and destination nodes.
BridgeNode originNode = connections.get(origin);
BridgeNode destinationNode = connections.get(destination);
// Add the destination node to origin's connection and the origin node to the destination node's
// connections if these connections do not already exist.
if(!originNode.connections.contains(destinationNode))
{
originNode.connections.add(destinationNode);
}
if(!destinationNode.connections.contains(originNode))
{
destinationNode.connections.add(originNode);
}
}
}
public String toString()
{
String toReturn = "Connections\n----------\n";
for(BridgeNode node : connections)
{
toReturn += node.getValue() + " [" + node.depth + "]: ";
for(BridgeNode child : node.connections)
{
toReturn += child.getValue() + " ";
}
toReturn += "\n";
}
return toReturn;
}
}