0

Problem I'm dealing with is pretty straightforward. Need to get simple true or false from a function.

My business domain with classes looks like this:

                                 A
                                / \
                               B   C
                                  / \
                                 D   E

Probably in future this will change, so it will not be a binary tree but it will still stay a tree, where each node can have multiple children.

Since I'm not very versed with all those fancy DS, I decide to relay on tutorials and libraries. The code I came up is going like so:

import org.jgrapht.DirectedGraph;
import org.jgrapht.GraphTests;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.AsUndirectedGraph;
import org.jgrapht.GraphTests;

    @GetMapping("/b/{vertices}")
    public Object test2(@PathVariable("vertices") String vertices) throws IOException {
    
    
        List<String> possibleVertices = Arrays.stream(vertices.split(",")).collect(Collectors.toList());
    
        DirectedGraph<String, DefaultEdge> directedGraph = new DefaultDirectedGraph<>(DefaultEdge.class);
    
        possibleVertices.forEach(directedGraph::addVertex);
    
        if (possibleVertices.contains("A") && possibleVertices.contains("B")){
            directedGraph.addEdge("A", "B");
        }
    
        if (possibleVertices.contains("A") && possibleVertices.contains("C")){
            directedGraph.addEdge("A", "C");
        }
    
        if (possibleVertices.contains("C") && possibleVertices.contains("D")){
            directedGraph.addEdge("C", "D");
        }
        if (possibleVertices.contains("C") && possibleVertices.contains("E")){
            directedGraph.addEdge("C", "E");
        }
    
        AsUndirectedGraph<String, DefaultEdge> unidirected = new AsUndirectedGraph<>(directedGraph);
    
        return GraphTests.isTree(unidirected );
    }

I tested all combinations and code is working as expected. However I do have couple of questions:

  1. Is this library meant to use it in this way? That is, is mapping between vertices meant to be done in this manner?
  2. Why do I need to convert from directed to undirected graph?
  3. Since I'm working with Spring Boot, is it safe to use @Bean for creating directedGraph, unidirected (possibly providing vertices to bean through lambda or something similar?) I do have doubts if those vertices with addVertex are not cached in some map within that object I'm invoking to construct the graph?
  4. Is creation of any *grpah with new using this library expansive?
Wrapper
  • 794
  • 10
  • 25
  • 1
    Why do you want Spring to come into this? – Louis Wasserman Feb 14 '22 at 00:45
  • Because, I'm working in Spring Boot, and part of the service I'm implementing requires from me to get true/false info from this function. So mainly because of question 3. and 4. I'm interested to know if it would be more optimal to place creation of graph into @Bean. – Wrapper Feb 14 '22 at 00:49
  • I recommend you solve the Spring integration and the graph analysis parts separately. – Louis Wasserman Feb 14 '22 at 00:53
  • any thoughts on why do you think spring is good/bad idea? and how would you handle graph analysis part – Wrapper Feb 14 '22 at 01:22
  • 1) I don't think Spring good idea vs bad idea comes into it. Its more a case of not restricting yourself to only using Spring-blessed solutions. And breaking your large problems down into smaller sub-problems that can be solved independently. 2) Calls for recommendations. All such questions are off-topic on StackOverflow. – Stephen C Feb 14 '22 at 01:37

0 Answers0