Home > Articles > Programming > Java

  • Print
  • + Share This
  • 💬 Discuss
Like this article? We recommend

Building the Graph

The complete graph in Figures 1 and 2 can be created and displayed using the code in Listing 2.

Listing 2 Building the graph

Graph myGraph = new Graph(7, false);
myGraph.insertEdge(new Edge(0, 1));
myGraph.insertEdge(new Edge(0, 2));
myGraph.insertEdge(new Edge(0, 5));
myGraph.insertEdge(new Edge(5, 3));
myGraph.insertEdge(new Edge(3, 4));
myGraph.insertEdge(new Edge(4, 6));
myGraph.insertEdge(new Edge(6, 1));

myGraph.graphDetails.show(myGraph);

Once the Listing 2 code runs, you should see the output shown in Listing 3 appear.

Listing 3 Displaying the graph contents

Graph details
0: 1 2 5
1: 0 6
2: 0
3: 4 5
4: 3 6
5: 0 3
6: 1 4

Listing 3 can be read as follows: the left number corresponds to a given vertex. The numbers after the colon indicate the vertices adjacent to the left hand vertex.

So, vertex 0 is adjacent to vertices 1, 2, and 5, respectively; as you can verify from Figures 1 and 2.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus