0

I am new to Graph theory in addition to this JGraphT (Java) library I'm using in order to implement a solution to a logistics issue I'm trying to solve. As such, I'm a little lost on the best way to tackle this problem I'm having to represent a shipment's path from Point A to Point C given the incoming data.

Given a list of Conveyance segments or Ordered Pairs, how do I represent this programmatically with the fewest possible edges?

Delivery 1 goes from Atlanta to Mumbai.

Delivery 2 goes from Atlanta to London.

Delivery 3 goes from London to Mumbai.

In my visual graph representation, I want to remove the explicit Atlanta to Mumbai edge and simply infer that from the other edges and represent it simply as:

Atlanta -> London -> Mumbai

I feel like there's likely an existing Path Algorithm that can be applied to solve this rather simple use case but I'm struggling to figure out which one given my relative newness to the subject matter. If my requirement was to remove excessive vertices rather than edges, then it seems like the ShortestPathAlgorithm would be of use here.

I can possibly identify the ultimate source and sink of my given pairs (i.e. Atlanta is the source and Mumbai is the sink) but don't want to go down the path of manually removing the edges if possible.

Current representation:

Current Representation

Desired representation:

enter image description here

I have created a class to get me close to implement the alternative Depth-first solution @JorisKinable mentions below but still not understanding why "Atlanta, Mumbai, and London" are listed in that order. If no weight is applied to the edges, what causes Mumbai to come before London in this scenario?

public final class Demo {

  public static void main(String[] args) throws Exception {

    // Create the graph object
    Graph<String, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);

    String atlanta = "Atlanta";
    String london = "London";
    String mumbai = "Mumbai";

    graph.addVertex(atlanta);
    graph.addVertex(london);
    graph.addVertex(mumbai);

    graph.addEdge(atlanta, london);
    graph.addEdge(london, mumbai);
    graph.addEdge(atlanta, mumbai);

    ComponentNameProvider<String> vertexIdProvider = name -> name;
    ComponentNameProvider<String> vertexLabelProvider = name -> name;

    String start = graph.vertexSet().stream().filter(r -> r.equals("Atlanta")).findAny().get();
    System.out.println("-- traverseGraph output");
    traverseGraph(graph, start);

    GraphExporter<String, DefaultEdge> exporter = new DOTExporter<>(vertexIdProvider, vertexLabelProvider, null);
    Writer writer = new StringWriter();
    exporter.exportGraph(graph, writer);
    System.out.println(writer.toString());
  }

  private static void traverseGraph(Graph<String, DefaultEdge> graph, String start) {
    Iterator<String> iterator = new DepthFirstIterator<>(graph, start);
    while (iterator.hasNext()) {
      String string = iterator.next();
      System.out.println(string);
    }
  }
}
daniel9x
  • 755
  • 2
  • 11
  • 28
  • This question has nothing to do with JGraphT. First you need to some the conceptual problem, then you can think of the implementation. – Joris Kinable Jul 24 '19 at 17:07
  • Is there a specific reason why you would remove the arc (Atlanta,Mumbai) as opposed to the arc (London,Mumbai)? – Joris Kinable Jul 24 '19 at 17:09
  • We're trying to represent a logical line a truck would take in dropping off the deliveries given a set of origin (source) and destination (sink) pairs. Apologies for referencing the implementation tool before even figuring out the conceptual solution. I am very new to both the theory and the chosen implementation. – daniel9x Jul 24 '19 at 17:51

1 Answers1

1

Currently the question is not stated precise enough to give an exact answer. It seems however that you can solve your problem through the following steps:

  1. Construct a directed graph with all arcs included. Add one additional node 's' to the graph which has outgoing arcs to all other nodes.
  2. Perform a Breadth First Search (BFS) starting from node 's'.
  3. Finally, remove node 's' as well as all edges which are not part BFS tree. You could also use Depth First Search instead of BFS, and remove all back edges, forward edges and cross edges.

All of this is easily accomplished in JGraphT, but that's a separate question.

Joris Kinable
  • 2,232
  • 18
  • 29
  • Thanks for the suggestion. I took a stab at the Depth First Iterator to understand more about that algorithm, but not understanding why Mumbai comes before London in that scenario (or why it wouldn't). See code example below. – daniel9x Jul 24 '19 at 19:02
  • You should have a look at the Pseudocode of the DFS algorithm on the linked Wikipedia page. DFS cannot distinguish between the solutions {(atlanta->london),(atlanta->mumbai)} and {(atlanta->london),(london->mumbai)}. Both of these solutions represent your problem with "the fewest possible edges" as per your question. – Joris Kinable Jul 24 '19 at 19:31