2021年1月19日星期二

terminal node of a simple network

I have to solve a weird exercise on terminal nodes in a simple network. The objective of this exercise is to find the terminal node(endpoint) of a simple network. In this simple network, each node is linked to at most one outgoing node in a unidirectional manner.

enter image description here

The image above is an example of a simple network

Implement the findNetworkEndpoint (startNodeId, fromIds, toIds) method which returns the Id of the last network node found starting from the startNodeId node and following the network links.

In the example above, the terminal node starting from node number 2 (or any node) is node number 5

fromIds et toIds are two arrays of the same length that describe the unidirectional links of the network(fromIds [i] is related to toIds [i])

In case you encounter a loop while traversing the network, the method should return the Id of the last node traversed before closing the loop.

Constraints:

  • 0 <number of links <10,000
  • a node cannot be tied directly to itself

So i came up with this code:

public class Solution_Node {            public static int findNetworkEndpoint(int startNodeId ,int[] fromIds,int[] toIds) {                    //Constraints of the statement           if((toIds.length<= 0)||(toIds.length > 10000))              {                  return 0;              }                      //conversion for manipulation of list () which contains the IndexOf () method           List<Integer> toIdsList = toIds.toList();      //Line 1           List<Integer> fromIdsList = fromIds.toList();  //Line 2             boolean endFound = false;      // my boolean if I have an end of a node           boolean lapEnded = false;      // my boolean if my search goes in circles           boolean startSearch = true;    // my boolean to indicate that I am at the start of my search             int startingNode;          int node = startingNode;           int indexNode = Integer.MIN_VALUE;   //because index 0 exists           int oldIndex = Integer.MIN_VALUE;   //because index 0 exists             while (!endFound && !lapEnded)  // as long as i haven't found the end node           {                               // or that I did not do a full tour                 if (!startSearch && (node == startingNode)) // if I am not at the start of my research               {                                           // and that I find the same node,                   lapEnded = true;                        // is that I made a turn                   indexNode = oldIndex;                   break;               }               if(startSearch)             //research has started               {                   startSearch = false;               }                 if(fromIdsList.contains(node))                                 {                   oldIndex = indexNode;                   indexNode = fromIdsList.indexOf(node);                   node = toIdsList[indexNode]; //Line 3               }               else               {                   endFound = true;               }             }           return indexNode;                    }                                public static void main(String[] args) {           int startNodeId = 6;           int n = 4;             int[] fromIds = new int[] { 4, 9, 6, 1 };           int[] toIds = new int[] { 9, 5, 1, 4 };           System.out.println((startNodeId));           System.out.println(n);             for (int i = 0; i < n; i++)           {               System.out.println("from : " + fromIds[i] + " -> " + toIds[i]);           }             int endPointId = findNetworkEndpoint(startNodeId, fromIds, toIds);           System.out.println("L'ID du dernier noeud : "+endPointId);        }    }  

When I run my code, the console displays the following output:

6

4

from : 4 -> 9

from : 9 -> 5

from : 6 -> 1

from : 1 -> 4

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

Cannot invoke toList() on the array type int[]  Cannot invoke toList() on the array type int[]  The type of the expression must be an array type but it resolved to List<Integer>    at folder.Solution_Node.findNetworkEndpoint(Solution_Node.java:16)  at folder.Solution_Node.main(Solution_Node.java:76)  

Apparently there will be errors on lines 1, 2 and 3

How can i fix that in order for my code to work and display the lastnode Id?

https://stackoverflow.com/questions/65801913/terminal-node-of-a-simple-network January 20, 2021 at 09:35AM

没有评论:

发表评论