Skip to content

Commit c21ae9a

Browse files
Graph Initialization outside private classes
1 parent ce945bc commit c21ae9a

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

DataStructures/Graphs/A_Star.java

+41-44
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,6 @@ private void addEdge (Edge edge) {
2727
this.graph[edge.getFrom()].add(new Edge(edge.getFrom(), edge.getTo(), edge.getWeight()));
2828
this.graph[edge.getTo()].add(new Edge(edge.getTo(), edge.getFrom(), edge.getWeight()));
2929
}
30-
31-
private void initializeGraph() {
32-
this.addEdge(new Edge(0, 19, 75)); this.addEdge(new Edge(0, 15, 140));
33-
this.addEdge(new Edge(0, 16, 118)); this.addEdge(new Edge(19, 12, 71));
34-
this.addEdge(new Edge(12, 15, 151));this.addEdge(new Edge(16, 9, 111));
35-
this.addEdge(new Edge(9, 10, 70)); this.addEdge(new Edge(10, 3, 75));
36-
this.addEdge(new Edge(3, 2, 120)); this.addEdge(new Edge(2, 14, 146));
37-
this.addEdge(new Edge(2, 13, 138)); this.addEdge(new Edge(2, 6, 115));
38-
this.addEdge(new Edge(15, 14, 80)); this.addEdge(new Edge(15, 5, 99));
39-
this.addEdge(new Edge(14, 13, 97)); this.addEdge(new Edge(5, 1, 211));
40-
this.addEdge(new Edge(13, 1, 101)); this.addEdge(new Edge(6, 1, 160));
41-
this.addEdge(new Edge(1, 17, 85)); this.addEdge(new Edge(17, 7, 98));
42-
this.addEdge(new Edge(7, 4, 86)); this.addEdge(new Edge(17, 18, 142));
43-
this.addEdge(new Edge(18, 8, 92)); this.addEdge(new Edge(8, 11, 87));
44-
45-
/*
46-
.x. node
47-
(y) cost
48-
- or | or / bidirectional connection
49-
50-
( 98)- .7. -(86)- .4.
51-
|
52-
( 85)- .17. -(142)- .18. -(92)- .8. -(87)- .11.
53-
|
54-
. 1. -------------------- (160)
55-
| \ |
56-
(211) \ .6.
57-
| \ |
58-
. 5. (101)-.13. -(138) (115)
59-
| | | /
60-
( 99) ( 97) | /
61-
| | | /
62-
.12. -(151)- .15. -(80)- .14. | /
63-
| | | | /
64-
( 71) (140) (146)- .2. -(120)
65-
| | |
66-
.19. -( 75)- . 0. .10. -(75)- .3.
67-
| |
68-
(118) ( 70)
69-
| |
70-
.16. -(111)- .9.
71-
*/
72-
}
7330
}
7431

7532
private static class Edge {
@@ -117,13 +74,53 @@ private void printSolution () {
11774
}
11875
}
11976

77+
private static void initializeGraph(Graph graph, ArrayList<Integer> data) {
78+
for (int i = 0; i < data.size(); i+=4) {
79+
graph.addEdge(new Edge(data.get(i), data.get(i + 1), data.get(i + 2)));
80+
}
81+
/*
82+
.x. node
83+
(y) cost
84+
- or | or / bidirectional connection
85+
86+
( 98)- .7. -(86)- .4.
87+
|
88+
( 85)- .17. -(142)- .18. -(92)- .8. -(87)- .11.
89+
|
90+
. 1. -------------------- (160)
91+
| \ |
92+
(211) \ .6.
93+
| \ |
94+
. 5. (101)-.13. -(138) (115)
95+
| | | /
96+
( 99) ( 97) | /
97+
| | | /
98+
.12. -(151)- .15. -(80)- .14. | /
99+
| | | | /
100+
( 71) (140) (146)- .2. -(120)
101+
| | |
102+
.19. -( 75)- . 0. .10. -(75)- .3.
103+
| |
104+
(118) ( 70)
105+
| |
106+
.16. -(111)- .9.
107+
*/
108+
}
109+
120110
public static void main(String[] args) {
121111
//heuristic function optimistic values
122112
int[] heuristic = {366, 0, 160, 242, 161, 178, 77, 151, 226,
123113
244, 241, 234, 380, 98, 193, 253, 329, 80, 199, 374};
124114

125115
Graph graph = new Graph(20);
126-
graph.initializeGraph();
116+
ArrayList<Integer> graphData = new ArrayList<>(Arrays.asList(0, 19, 75, null,
117+
0, 15, 140, null, 0, 16, 118, null, 19, 12, 71, null, 12, 15, 151, null,
118+
16, 9, 111, null, 9, 10, 70, null, 10, 3, 75, null, 3, 2, 120, null,
119+
2, 14, 146, null, 2, 13, 138, null, 2, 6, 115, null, 15, 14, 80, null,
120+
15, 5, 99, null, 14, 13, 97, null, 5, 1, 211, null, 13, 1, 101, null,
121+
6, 1, 160, null, 1, 17, 85, null, 17, 7, 98, null, 7, 4, 86, null,
122+
17, 18, 142, null, 18, 8, 92, null, 8, 11, 87));
123+
initializeGraph(graph, graphData);
127124

128125
PathAndDistance solution = aStar(3, 1, graph, heuristic);
129126
solution.printSolution();

0 commit comments

Comments
 (0)