Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:11:45+00:00 2026-05-15T20:11:45+00:00

I am writing a Graph library that has both adjacency list and matrix implementations.

  • 0

I am writing a Graph library that has both adjacency list and matrix implementations. Here is some code I came across in a Java data structures textbook:

static void floyd(Graph<V,E> g) 
// post: g contains edge (a,b) if there is a path from a to b 
{
     Iterator<V> witer = g.iterator();   //vertex iterator
     while (witer.hasNext())
     {
        Iterator<V> uiter = g.iterator(); 
        V w = witer.next(); 
        while (uiter.hasNext())
        {
            Iterator<V> viter = g.iterator();
            V u = uiter.next(); 
            while (viter.hasNext())
            {
                V v = viter.next(); 
                if (g.containsEdge(u,w) && g.containsEdge(w,v)) 
                {
                     Edge<V,E> leg1 = g.getEdge(u,w);
                     Edge<V,E> leg2 = g.getEdge(w,v); 
                     int leg1Dist = leg1.label(); 
                     int leg2Dist = leg2.label(); 
                     int newDist = leg1Dist+leg2Dist;

                     if (g.containsEdge(u,v)) 
                     {
                         Edge<V,E> across = g.getEdge(u,v); 
                         int acrossDist = across.label(); 
                         if (newDist < acrossDist)
                           across.setLabel(newDist); 
                      } 
                      else 
                      {
                           g.addEdge(u,v,newDist);
                       }
                  }
             }
         }
     }

But it seems like it is just overwriting the current edge with with the “shortest”. Is this interpretation correct? I could use some clarification here.

Note: Here is some of the Edge class:

public class Edge
{
/**
 * Two element array of vertex labels.
 * When necessary, first element is source.
 */
protected Object[] vLabel;  // labels of adjacent vertices
/**
 * Label associated with edge.  May be null.
 */
protected Object label;     // edge label
/**
 * Whether or not this edge has been visited.
 */
protected boolean visited;  // this edge visited
/**
 * Whether or not this edge is directed.
 */
protected boolean directed; // this edge directed

/**
 * Construct a (possibly directed) edge between two labeled
 * vertices.  When edge is directed, vtx1 specifies source.
 * When undirected, order of vertices is unimportant.  Label
 * on edge is any type, and may be null.
 * Edge is initially unvisited.
 *
 * @post edge associates vtx1 and vtx2; labeled with label
 *       directed if "directed" set true
 *
 * @param vtx1 The label of a vertex (source if directed).
 * @param vtx2 The label of another vertex (destination if directed).
 * @param label The label associated with the edge.
 * @param directed True iff this edge is directed.
 */
public Edge(Object vtx1, Object vtx2, Object label,
            boolean directed)
{
    vLabel = new Object[2];
    vLabel[0] = vtx1;
    vLabel[1] = vtx2;
    this.label = label;
    visited = false;
    this.directed = directed;
}

/**
 * Returns the first vertex (or source if directed).
 *
 * @post returns first node in edge
 * 
 * @return A vertex; if directed, the source.
 */
public Object here()
{
    return vLabel[0];
}

/**
 * Returns the second vertex (or source if undirected).
 *
 * @post returns second node in edge
 * 
 * @return A vertex; if directed, the destination.
 */
public Object there()
{
    return vLabel[1];
}

/**
 * Sets the label associated with the edge.  May be null.
 *
 * @post sets label of this edge to label 
 * 
 * @param label Any object to label edge, or null.
 */
public void setLabel(Object label)
{
    this.label = label;
}

/**
 * Get label associated with edge.
 *
 * @post returns label associated with this edge
 * 
 * @return The label found on the edge.
 */
public Object label()
{
    return label;
}
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:11:45+00:00Added an answer on May 15, 2026 at 8:11 pm

    It would be easier to understand what you’re doing if you use a matrix to store the result in a matrix. Consider the following simple weighted graph:

           2
    1 +---------+ 2
      |\        |
      | -\      |
    3 |   -\5   | 2
      |     -\  |
      |       -\|
    3 +---------+ 4
            4
    

    Now consider this implementation of the Floyd-Warshall algorithm:

    public Matrix floyd() {
      Matrix m = new Matrix(numVertices, numVertices, Integer.MAX_VALUE);
      for (int i = 1; i<=numVertices; i++) {
        EdgeNode edge = edges[i];
        while (edge != null) {
          m.setData(i, edge.getY(), edge.getWeight());
          edge = edge.getNext();
        }
        m.setData(i, i, 0);
      }
      for (int i = 1; i <= numVertices; i++) {
        for (int j = 1; j <= numVertices; j++) {
          for (int k = 1; k <= numVertices; k++) {
            if (m.getData(i, j) < Integer.MAX_VALUE && m.getData(i, k) < Integer.MAX_VALUE) {
              int through = m.getData(i, j) + m.getData(i, k);
              if (through < m.getData(j, k)) {
                m.setData(j, k, through);
              }
            }
          }
        }
      }
      return m;
    }
    

    The first part of this seeds the matrix result with Integer.MAX_VALUE. Putting 0 here would yield an incorrect result but using values of 1 and 0 (respectively) would work fine for an unweighted graph. Integer.MAX_VALUE is there simply for correct minimal value checks.

    The second part is the key part of the algorithm. It looks at the distance between two points (i,k) comparing it to the distance of (i,j) + (j,K) for all vertices j. If the indirect path is less it is substituted into the matrix as the shortest path and so on.

    The result of this algorithm on the above (very simple) graph is:

    [ 0 2 3 5 ]
    [ 2 0 5 3 ]
    [ 3 5 0 4 ]
    [ 5 3 4 0 ]
    

    What this tells you is the shortest distance between any pair of vertices. Note: I’ve seeded the result of (i,i) to 0 as you can argue the distance of any node to itself is 0. You can take out that assumption easily enough, yielding this result:

    [ 4 2 3 5 ]
    [ 2 4 5 3 ]
    [ 3 5 6 4 ]
    [ 5 3 4 6 ]
    

    So node 3 to itself is a distance of 6 as it traverses 3->1->3 as the shortest path. This would get a lot more interesting in a directed graph, which Floyd’s can handle.

    Floyd’s is an O(n3) algorithm. It won’t reconstruct the actual path between each pair of points, just the total distance (weight). You can use Dijkstra’s algorithm between each vertex pair to construct the actual paths, which interestingly enough is also O(n3) but tends to be slower in real world usage as the calculations of Floyd’s are pretty simple.

    Your algorithm uses an adjacency list instead of a matrix to implement this algorithm, which confuses the issue slightly. My version uses Integer.MAX_VALUE as a sentinel value to indicate no path (yet calculated) whereas yours uses the absence of an edge for the same thing. Other than that, it’s exactly the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing documentation in html requires some code examples. What to do with characters that
I am writing on a graph library that should read the most common graph
I'm writing some data acquisition software and need a gui plotting library that is
I'm writing a plotting/graphing library for Javascript and came across what seems to be
Can anyone point me to some good Java library (jar) that has some more
I have begun writing some code for a library I need. The following code
I am writing a Graph-class using boost-graph-library. I use custom vertex and edge properties
Writing Classic ASP code in JScript has a lot going for it: more humane
I am writing a simple widget that renders a canvas participation graph just like
I am writing a program in which at some point a graph is plotted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.