Question

i have read file function which will read a txt file. after i read it, i put the value to a list. below is sample data:

public void readDisplayClient()
{   
DisplayClient dc = null;
try
{
    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("ClientData.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    String [] values = new String[3];
    int counter = 0;
    int clientCounter = 0;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   
    {
        // Print the content on the console

        String delims = ";";
        String[] tokens = strLine.split(delims);

        if(counter == 0)//Reading total clients
        {                       
            totalClient = Integer.parseInt(tokens[0]);
            counter++;
        }
        else
        {
            //System.out.println("Test " + counter + " " + tokens.length);
            for (int i = 0; i < tokens.length; i++)
            {
                    values[i] = tokens[i];
                    //System.out.println(tokens[i]);
            }
            dc = new DisplayClient(clientCounter,values[0],values[1],values[2]);
            //dc.printDetails(); // save the connected nodes details to logger txt file.
            clientList.add(dc);
            clientCounter++;
        }
    }
    //Close the input stream
    in.close();
    ss.setTotalClient(totalClient);
    ss.setClientList(clientList);
    //ss.printClientList();
}
catch (Exception e)
{//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}
}

my txt data file will be something like :

2// total 2 conections

0;1;500; // Node 0 connect to Node 1 with 500 kbps

1;2;500 // Node 1 connect to Node 2 with 500 kbps

when node 1 is connected to node 2, it is actually also connect to node 0 as well. Is this able to put it on a hashmap ??

i am a bit confused on it. thanks in advance for help.

Was it helpful?

Solution

There are various ways to do it. Since each edge has a speed you can have a class for each node and a class for each edge:

Create a class which represents your node. It should carry the data (node id), and which connections (edges) it has going out from it (since its a directed graph).

public class Node
{
  private int nodeId;
  private List<Connection> outboundConnections = new ArrayList<>();

  public Node(int nodeId)
  {
    this.nodeId = nodeId;
  }

  public void addConnection(Connection connection)
  {
    this.outboundConnections.add(connection);
  }

  //... setters and getters
}

Then create a class which represents the edges, including the data for the connection and which node it connects to (destination, since its a directed graph):

   public class Connection
   {
      private int speedKbps;
      private Node endNode;

      public Connection(Node endNode, int speedKbps)
      {
        this.endNode = endNode;
        this.speedKbps = speedKbps;
      }

      //... setters and getters
   }

In your class you keep a Map of all the created nodes (best if it is a member of the class but depends on what you're doing).

Map<Integer, Node> nodes = new TreeMap<>(); 

Then for each line in your loop you can do:

int fromNodeId = new Integer(values[0]);
int toNodeId = new Integer(values[1]);
int speedKbps = new Integer(values[2]);

Node fromNode = nodes.get(fromNodeId);
if (fromNode == null) //if we haven't seen this node already, create it and add it to the map
{
   fromNode = new Node(fromNodeId);
   nodes.put(fromNodeId, fromNode);
}

Node toNode = nodes.get(toNodeId);
if (toNode == null) //if we haven't seen this node already, create it and add it to the map
{
   toNode = new Node(toNodeId);
   nodes.put(fromNodeId, toNode);
}

Connection connection = new Connection(toNode, speedKbps);
fromNode.addConnection(connection);

This approach works for a directed graph, assuming you want to traverse from the one of the nodes in the direction of the arrows.

There are of course other alternatives (for example storing it as a large 2D matrix, with the kbps as the number in the matrix and the node numbers on the left representing 'from' nodes, and the node numbers on the top the 'to' nodes, or the other way round).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top