readfile并映射到有向图

时间:2012-12-06 11:52:06

标签: java list readfile directed-graph

我有读取文件功能,它将读取一个txt文件。在我读完之后,我将值放到列表中。以下是样本数据:

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());
}
}

我的txt数据文件将类似于:

2 //总共2个连接

0; 1; 500; //节点0以500 kbps

连接到节点1

1; 2; 500 //节点1以500 kbps

连接到节点2

当节点1连接到节点2时,它实际上也连接到节点0。这可以把它放在一个hashmap上吗?

我对它有点困惑。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

有各种方法可以做到这一点。由于每个边都有一个速度,因此每个节点都有一个类,每个边有一个类:

创建一个代表您的节点的类。它应该携带数据(节点id),以及它从它出来的连接(边缘)(因为它是有向图)。

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
}

然后创建一个表示边的类,包括连接的数据和连接的节点(目的地,因为它是有向图):

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

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

      //... setters and getters
   }

在你的班级中,你会保留所有已创建节点的地图(如果它是班级成员,则最好,但取决于你正在做什么)。

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

然后,循环中的每一行都可以:

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);

这种方法适用于有向图,假设您想要沿箭头方向从其中一个节点进行遍历。

当然还有其他替代方案(例如将其存储为大型2D矩阵,其中kbps为矩阵中的数字,左侧的节点编号代表'from'节点,节点编号位于顶部'到'节点,或反过来说。)