Home > Articles > Programming > Java

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

Looking at the Graph Code

Listing 4 illustrates the code used to insert an edge into a graph.

Listing 4 Edge insertion

void insertEdge(Edge e)
{
  int v = e.v;
  int w = e.w;

  if (adjacencies[v][w] == false)
  {
    EdgeCount++;
    adjacencies[v][w] = true;
  }

  if (!digraph)
  {
    adjacencies[w][v] = true;
  }
}

The two-dimensional array adjacencies is used to record which vertices are joined by an edge. A value of false indicates that no edge is present. If no edge is present, then the number of edges is incremented and the associated entry in adjacencies is set to true.

The test on the value of digraph is required because each edge in a directional graph has a notional sense. That is, if Figure 1 were a digraph (which it isn’t), then vertex 0 would point to vertex 5, and vertex 5 would point back to vertex 0.

So, that’s edge insertion. But what about edge removal?

  • + Share This
  • 🔖 Save To Your Account