Home > Articles > Programming > Java

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

Putting It All Together

Listing 6 illustrates the actions of the following:

  • Creating a graph
  • Adding some edges
  • Displaying the graph
  • Removing some edges
  • Displaying the graph

Listing 6 The full workflow from creation to deletion

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.display(myGraph);
myGraph.removeEdge(new Edge(0, 1));
myGraph.graphDetails.display(myGraph);

In Listing 6, I create an object of the class Graph. I then add some edges and display the resulting graph.

Next, I remove an edge and then redisplay the graph. Listing 7 illustrates the resulting program output.

Listing 7 The graph in action

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

Notice the change related to vertex 0 after I remove the object Edge(0, 1).

  • + Share This
  • 🔖 Save To Your Account