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 466781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:30:53+00:00 2026-05-12T23:30:53+00:00

Preface: I know that there are high quality graph APIs available. I’m interested in

  • 0

Preface: I know that there are high quality graph APIs available. I’m interested in writing my own for self-improvement.

This is my function to add nodes:

    public void addNode(Vertex v, Collection<Edge> neighbors) {

        int originalSize = size();

        if (head == null) {
            head = v;
        }
        else {
            Collection<Edge> inEdges = new ArrayList<Edge>();
            inEdges.addAll(neighbors);

            traverseGraphToAdd(head, inEdges, v);
        }

        assert originalSize + 1 == size() : 
                        String.format("adding operation failed. original size: %d, current size: %d", originalSize, size());
    }
private void traverseGraphToAdd(Vertex start, Collection<Edge> inEdges, Vertex toAdd) {
        Iterator<Edge> iter = inEdges.iterator();
        Edge e;
        while (iter.hasNext()) {
            e = iter.next();
            if (e.getSource().equals(start)) {
                start.addEdge(e);
                iter.remove();
            }
            else if (! directionalEdges && e.getSink().equals(start)) {
                start.addEdge(e);
                iter.remove();
            }
        }
        if (inEdges.size() > 0) { //otherwise there's no point in continuing to search
            for (Edge arc : start.getOutEdges()) {
                traverseGraphToAdd(arc.getSink(), inEdges, toAdd);
            }
        }
    }

Size and its dependencies:

public int size() {
    int count = 0;
    if (head == null) {
        return 0;
    }
    else {
        count = countNodes(head);
    }
    clearVisited();
    return count;
}

private int countNodes(Vertex start) {
    int result = 1;
    start.setVisited(true);
    for (Edge e: start.getOutEdges()) {
        if (! e.getSink().isVisited()) {
            result += countNodes(e.getSink());
        }
    }
    return result;
}

private void clearVisited() {
    if (head != null) {
        clearNode(head);
    }
}

private void clearNode(Vertex start) {
    start.setVisited(false);
    for (Edge e: start.getOutEdges()) {
        if (e.getSink().isVisited()) {
            clearNode(e.getSink());
        }
    }
}

The Edge class:

public Edge(Vertex source, Vertex sink, int weight) {
    this.source = source;
    this.sink = sink;
    this.weight = weight;
}

The following call works:

g.addNode(ftw, new HashSet<Edge>()); //first node - empty edges
g.addNode(odp, Arrays.asList(new Edge(ftw, odp, 3))); //link new node to one already in the graph

This does not:

g.addNode(tlt, Arrays.asList(new Edge(tlt, ftw, 2)));

In this one, the first argument of the Edge constructor is not the node already in the graph. I try to rectify this in addNode with the following (repeated from above):

if (e.getSource().equals(start)) { /*... */ }
else if (! directionalEdges && e.getSink().equals(start)) { /*... */ }

directionalEdges is a class field that determines whether or not this graph is directional or not.

However, this causes assertion errors:

Exception in thread "main" java.lang.AssertionError: adding operation failed. original size: 1, current size: 1

What is going on here?

  • 1 1 Answer
  • 0 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-12T23:30:54+00:00Added an answer on May 12, 2026 at 11:30 pm

    The graph you’re trying to create in your example looks like this:

    tlt -> ftw -> odp

    After creating ftw -> odp, you should (and do, I believe) have head == ftw. After adding tlt, you should have head == tlt if you want your traversal algorithm to work properly. But in the code you’ve shown us, there is only one place where head is assigned to, and that happens only in the condition when head == null, in the fifth line of addNode(). Therefore, head doesn’t change when you add tlt, and so traverseGraphToAdd() therefore starts form ftw instead of tlt as you intend for it to.

    You have a more general problem here, however, namely that your code isn’t able to handle directed graphs which aren’t rooted (that is, they have more than one source node.) Consider what would happen if you wanted a graph like this one:

    a -> b <- c

    I think you’d have a problem with this, since you no longer have a single head.

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

Sidebar

Related Questions

I will preface this question with the disclaimer that I know there are many
To preface this, I know there are discussions on this in various places. Half
Preface I'm learning about computer math by writing and refining my own BigInt library.
I'll preface this with the fact that I'm new to RoR. I have a
We are using WSS 3 SP2. I'd like to preface this by saying that
This is a C# winforms app. Preface: I am creating a form that will
Preface: I know this is an unusual/improper way to do this. I can do
As a preface, I am brand new to using SQL Server 2005; I know
Let me preface this (Because I know I'll get this eventually in the responses)
Let me preface this question with first stating that I'm new to GUI interfaces:

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.