Skip to main content
Bounty Awarded with 50 reputation awarded by Rahul Wadhwani
added 23 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
public class Vertex {
    public final Map<Vertex, Integer> edges;
    public final String name;

    public Vertex(String name) {
        edges = new HashMap<>();
        this.name = name;
    }

    public Vertex addEdge(Vertex other, int weight) {
        edges.put(other, weight);
        return this;
    }

    public static void main(String[] args) {
        Vertex start = new Vertex("start");
        Vertex otherA = new Vertex("otherA");
        Vertex otherB = new Vertex("otherB");
        Vertex end = new Vertex("end");

        start.addEdge(otherA, 1).addEdge(otherB, 2);
        otherA.addEdge(otherB, 3).addEdge(end, 4);
        otherB.addEdge(end, 5);
    } 

    // Other stuff.
}
public class Vertex {
    public final Map<Vertex, Integer> edges;
    public final String name;

    public Vertex(String name) {
        edges = new HashMap<>();
        this.name = name;
    }

    public Vertex addEdge(Vertex other, int weight) {
        edges.put(other, weight);
        return this;
    }

    public static void main(String[] args) {
        Vertex start = new Vertex("start");
        Vertex otherA = new Vertex("otherA");
        Vertex otherB = new Vertex("otherB");
        Vertex end = new Vertex("end");

        start.addEdge(otherA, 1).addEdge(otherB, 2);
        otherA.addEdge(otherB, 3).addEdge(end, 4);
        otherB.addEdge(end, 5);
    }
}
public class Vertex {
    public final Map<Vertex, Integer> edges;
    public final String name;

    public Vertex(String name) {
        edges = new HashMap<>();
        this.name = name;
    }

    public Vertex addEdge(Vertex other, int weight) {
        edges.put(other, weight);
        return this;
    }

    public static void main(String[] args) {
        Vertex start = new Vertex("start");
        Vertex otherA = new Vertex("otherA");
        Vertex otherB = new Vertex("otherB");
        Vertex end = new Vertex("end");

        start.addEdge(otherA, 1).addEdge(otherB, 2);
        otherA.addEdge(otherB, 3).addEdge(end, 4);
        otherB.addEdge(end, 5);
    } 

    // Other stuff.
}
added 888 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
public class Vertex {
    public final Map<Vertex, Integer> edges;
    public final String name;

    public Vertex(String name) {
        edges = new HashMap<>();
        this.name = name;
    }

    public Vertex addEdge(Vertex other, int weight) {
        edges.put(other, weight);
        return this;
    }

    public static void main(String[] args) {
        Vertex start = new Vertex("start");
        Vertex otherA = new Vertex("otherA");
        Vertex otherB = new Vertex("otherB");
        Vertex end = new Vertex("end");

        start.addEdge(otherA, 1).addEdge(otherB, 2);
        otherA.addEdge(otherB, 3).addEdge(end, 4);
        otherB.addEdge(end, 5);
    }
}

This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting andto the ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.

I've found this tutorial that has a very similar approach to what you did. Maybe you can find some impressions there as well.

This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting and ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.

public class Vertex {
    public final Map<Vertex, Integer> edges;
    public final String name;

    public Vertex(String name) {
        edges = new HashMap<>();
        this.name = name;
    }

    public Vertex addEdge(Vertex other, int weight) {
        edges.put(other, weight);
        return this;
    }

    public static void main(String[] args) {
        Vertex start = new Vertex("start");
        Vertex otherA = new Vertex("otherA");
        Vertex otherB = new Vertex("otherB");
        Vertex end = new Vertex("end");

        start.addEdge(otherA, 1).addEdge(otherB, 2);
        otherA.addEdge(otherB, 3).addEdge(end, 4);
        otherB.addEdge(end, 5);
    }
}

This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting to the ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.

I've found this tutorial that has a very similar approach to what you did. Maybe you can find some impressions there as well.

added 1078 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12

Another think you could think about is not having indexing at all because it's not relevant to the algorithm. The algorithm only needs to know the first vertex. The first vertex is always the one that is not the destination of any relationship, or rather edge. The final vertex is the one that is not the source for any relationship, or rather edge. This will ensure that the graph organization is fully representedrepresentable by the data structure itself. A class to represent the graph would become redundant. You can keep a variable to give the vertices good names for printing the result.

This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting and ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.

Another think you could think about is not having indexing at all because it's not relevant to the algorithm. The algorithm only needs to know the first vertex. The first vertex is always the one that is not the destination of any relationship, or rather edge. The final vertex is the one that is not the source for any relationship, or rather edge. This will ensure that the graph organization is fully represented by the data structure itself. A class to represent the graph would become redundant. You can keep a variable to give the vertices good names for printing the result though.

Another think you could think about is not having indexing at all because it's not relevant to the algorithm. The algorithm only needs to know the first vertex. The first vertex is always the one that is not the destination of any relationship, or rather edge. The final vertex is the one that is not the source for any relationship, or rather edge. This will ensure that the graph organization is fully representable by the data itself. A class to represent the graph would become redundant. You can keep a variable to give the vertices good names for printing the result.

This would limit you to some degree though because you will have a hard time in changing a graph. You will have an even harder time to go through the graph in a different way than from the implicit starting and ending vertex. A class to manage that would become necessary. Anyways, these are all implementation details and depend on your requirements.

added 1078 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
Loading
deleted 63 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
Loading
added 17 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
Loading
added 17 characters in body
Source Link
akuzminykh
  • 394
  • 1
  • 12
Loading
Source Link
akuzminykh
  • 394
  • 1
  • 12
Loading