- 1
How to label vertices in undirected graph in java
I need to build a weighted Undirected graph. I'm having a hard time figuring out how to label the vertices in my graph with the corresponding weights. The vertices are label city names---> COLUMBUS,MIAMI,SAN FRANCISCO, HOUSTON, CHARLESTON. The weights are listed below COLUMBUS edges: Miami-52 , Charleston- 96, SAN FRANCISCO -108 CHARLESTON edges: Columbus, Houston-45 HOUSTON edges: SANFRANCISCO-75,Charleston MIAMI edges: Columbus Any suggestion on how to code the following ?
1 Answer
0
// eg
class Vertex { Vertex[] edges; }
Vertex
Columbus = new Vertex(),
Miami = new Vertex(),
SanFrancisco = new Vertex(),
Houston = new Vertex(),
Charleston = new Vertex();
{
Columbus .edges = new Vertex[] {Miami, Charleston, SanFrancisco};
Miami .edges = new Vertex[] {Columbus};
SanFrancisco.edges = new Vertex[] {Columbus, Houston};
Houston .edges = new Vertex[] {SanFrancisco, Charleston};
Charleston .edges = new Vertex[] {Columbus, Houston};
}
Vertex[] vertices = { Columbus, Miami, SanFrancisco, Houston, Charleston };