Java Reference
In-Depth Information
public
String getMayor() {
return
mayor;
}
public void
setMayor(String mayor) {
this
.mayor = mayor;
}
public void
setPopulation(
int
population) {
this
.population = population;
}
}
The vertices can be conveniently labeled using natural numbers 0, 1, 2,
c
,
n
1, for a
graph for
n
vertices. Thus,
vertices[0]
represents
"Seattle"
,
vertices[1]
represents
"San Francisco"
, and so on, as shown in FigureĀ 28.5.
-
vertices[0]
Seattle
vertices[1]
San Francisco
vertices[2]
Los Angeles
vertices[3]
Denver
vertices[4]
Kansas City
vertices[5]
Chicago
vertices[6]
Boston
vertices[7]
New York
vertices[8]
Atlanta
vertices[9]
Miami
vertices[10]
Dallas
vertices[11]
Houston
F
IGURE
28.5
An array stores the vertex names.
Note
You can reference a vertex by its name or its index, whichever is more convenient. Obvi-
ously, it is easy to access a vertex via its index in a program.
reference vertex
28.3.2 Representing Edges: Edge Array
The edges can be represented using a two-dimensional array. For example, you can store all
the edges in the graph in FigureĀ 28.1 using the following array:
int
[][] edges = {
{
0
,
1
}, {
0
,
3
}, {
0
,
5
},
{
1
,
0
}, {
1
,
2
}, {
1
,
3
},
{
2
,
1
}, {
2
,
3
}, {
2
,
4
}, {
2
,
10
},
{
3
,
0
}, {
3
,
1
}, {
3
,
2
}, {
3
,
4
}, {
3
,
5
},
{
4
,
2
}, {
4
,
3
}, {
4
,
5
}, {
4
,
7
}, {
4
,
8
}, {
4
,
10
},
{
5
,
0
}, {
5
,
3
}, {
5
,
4
}, {
5
,
6
}, {
5
,
7
},
Search WWH ::
Custom Search