Java Reference
In-Depth Information
24 };
25
26 WeightedGraph<String> graph1 =
27
new
WeightedGraph<>(vertices, edges);
28 System.out.println(
"The number of vertices in graph1: "
29 + graph1.getSize());
30 System.out.println(
"The vertex with index 1 is "
31 + graph1.getVertex(
1
));
32 System.out.println(
"The index for Miami is "
+
33 graph1.getIndex(
"Miami"
));
34 System.out.println(
"The edges for graph1:"
);
35
create graph
graph1.printWeightedEdges();
print edges
36
37 edges =
new int
[][] {
38 {
0
,
1
,
2
}, {
0
,
3
,
8
},
39 {
1
,
0
,
2
}, {
1
,
2
,
7
}, {
1
,
3
,
3
},
40 {
2
,
1
,
7
}, {
2
,
3
,
4
}, {
2
,
4
,
5
},
41 {
3
,
0
,
8
}, {
3
,
1
,
3
}, {
3
,
2
,
4
}, {
3
,
4
,
6
},
42 {
4
,
2
,
5
}, {
4
,
3
,
6
}
43 };
44 WeightedGraph<Integer> graph2 =
new
WeightedGraph<>(edges,
5
);
45 System.out.println(
"\nThe edges for graph2:"
);
46
edges
create graph
graph2.printWeightedEdges();
print edges
47 }
48 }
The number of vertices in graph1: 12
The vertex with index 1 is San Francisco
The index for Miami is 9
The edges for graph1:
Vertex 0: (0, 1, 807) (0, 3, 1331) (0, 5, 2097)
Vertex 1: (1, 2, 381) (1, 0, 807) (1, 3, 1267)
Vertex 2: (2, 1, 381) (2, 3, 1015) (2, 4, 1663) (2, 10, 1435)
Vertex 3: (3, 4, 599) (3, 5, 1003) (3, 1, 1267)
(3, 0, 1331) (3, 2, 1015)
Vertex 4: (4, 10, 496) (4, 8, 864) (4, 5, 533) (4, 2, 1663)
(4, 7, 1260) (4, 3, 599)
Vertex 5: (5, 4, 533) (5, 7, 787) (5, 3, 1003)
(5, 0, 2097) (5, 6, 983)
Vertex 6: (6, 7, 214) (6, 5, 983)
Vertex 7: (7, 6, 214) (7, 8, 888) (7, 5, 787) (7, 4, 1260)
Vertex 8: (8, 9, 661) (8, 10, 781) (8, 4, 864)
(8, 7, 888) (8, 11, 810)
Vertex 9: (9, 8, 661) (9, 11, 1187)
Vertex 10: (10, 11, 239) (10, 4, 496) (10, 8, 781) (10, 2, 1435)
Vertex 11: (11, 10, 239) (11, 9, 1187) (11, 8, 810)
The edges for graph2:
Vertex 0: (0, 1, 2) (0, 3, 8)
Vertex 1: (1, 0, 2) (1, 2, 7) (1, 3, 3)
Vertex 2: (2, 3, 4) (2, 1, 7) (2, 4, 5)
Vertex 3: (3, 1, 3) (3, 4, 6) (3, 2, 4) (3, 0, 8)
Vertex 4: (4, 2, 5) (4, 3, 6)
The program creates
graph1
for the graph in FigureĀ 29.1 in lines 3-27. The vertices for
graph1
are defined in lines 3-5. The edges for
graph1
are defined in lines 7-24. The edges
are represented using a two-dimensional array. For each row
i
in the array,
edges[i][0]
and
edges[i][1]
indicate that there is an edge from vertex
edges[i][0]
to vertex
Search WWH ::
Custom Search