0

Preview of data to process:

A
B
C
D
E

A,B
B,C
D,E

First part are nodes (points in software) and the second part (after empty line) are connections between nodes.

This file is loaded into two ArrayLists, which are separated (nodes and connections). All I need to do is merge (if one from nodes are same as in other connections) that connections between them.

public class ConsoleProcessor implements IntConsoleProcessor {
 List<String> nodeA = new ArrayList<>();
 List<String> nodeB = new ArrayList<>();

  //From ArrayList split original data to two arrays (point A & B)

  private void splitConnections(ArrayList<String> connections){
      for(String str:connections)
      {
          String[] nodes = str.split(",");
          nodeA.add(nodes[0]);
          nodeB.add(nodes[1]);
      }
      //Console print for fast check of results
      System.out.println(nodeA);
      System.out.println(nodeB);
  }

  public void getResults(ArrayList<String> nodes, ArrayList<String> connections){
      splitConnections(connections);
      //need to continue somehow

  }
 }

As result I want to print out to console connections A,B,C (or something like that) and E,D (or D,E).

Wanted result:

Connection counter: 2

E,D
C,A,B

Actual result is only printing out nodeA and nodeB from connections

[A, B, E]
[B, C, D]
  • Possible duplicate of [Finding all disconnected subgraphs in a graph](https://stackoverflow.com/questions/1348783/finding-all-disconnected-subgraphs-in-a-graph) – MTCoster Jan 21 '19 at 23:50
  • can you explain with an example clearly – Ryuzaki L Jan 21 '19 at 23:56
  • @Deadpool Preview of wanted result added. – Martin Kadlcek Jan 22 '19 at 00:01
  • sorry it is not clear can you explain this `All I need to do is merge (if one from nodes are same as in other connections) that connections between them.` and how connection matches with Node? – Ryuzaki L Jan 22 '19 at 00:08
  • @Deadpool - okay, I'm sorry. You have some nodes in data file (exactly `A,B,C,D,E`), which I need to make connected based on coordinates of connection (like `A,B` - so **we are connecting node A with node B**). If I read another coordinates, I see there B,C (connection of node B with node C), but I have connected node B already with node A. As result **I need connection node A with node B and with node C** (print to console something like `A,B,C`, which is way of connection between nodes). – Martin Kadlcek Jan 22 '19 at 00:14
  • **Solved** with [combining list of strings with a common element](https://stackoverflow.com/questions/22271776/combining-list-of-strings-with-a-common-element) – Martin Kadlcek Jan 22 '19 at 02:45

1 Answers1

0

The structure you are referring to is called graph, with vertices (what you call nodes) and edges (what you call connections).

What you are (supposedly) trying to compute is finding all the connected components of the graph. There are multiple algorithms to implement this but a trivial approach is just by searching for all vertices connected to a specific vertex and create a component accordingly, something like:

for each vertex A in graph
  if A is already visited skip to next
  mark A as visited
  for each edge A,B in graph
    if B is in a connected component 
      add A to the connected component
    else
      create new connected component with A and B

You should also take care of managing single vertex components but it's just to give you the general idea behind the solution.

Jack
  • 131,802
  • 30
  • 241
  • 343