简体   繁体   English

Java中具有未知顶点数的无向图

[英]Undirected graph in Java with unknown number of vertices

I am trying to create an undirected graph in Java with a "live feed" of vertices. 我正在尝试在Java中使用顶点的“实时订阅源”创建无向图。 I have the vertices coming in from a txt file. 我有一个来自txt文件的顶点。 The file is structured like this, 该文件的结构如下:

1 2 #connect node 1 with node 2

2 3 #connect node 1 with node 2

and so on. 等等。 I have decided to create an ArrayList of ArrayList (a 2d ArrayList) to store the nodes and edges in an array list. 我决定创建一个ArrayList的ArrayList(一个2d ArrayList),以将节点和边存储在数组列表中。 My requirement is that I have to maintain constant time in checking if there exists a path between two edges or no. 我的要求是我必须保持恒定的时间来检查两个边缘之间是否存在路径或不存在。 I haven't really used 2d ArrayList and any head-start from you guys would be really appreciated. 我还没有真正使用过2D ArrayList,因此非常感谢你们的领先。

Here is my point of view of working with graphs and my experience with them. 这是我使用图形的观点以及我在图形方面的经验。

First 第一

A graph is a graph, not an array, not a matrix, not of these. 图是图,不是数组,不是矩阵,不是这些。 Why am I saying that? 我为什么这么说? Because the structure of a graph could be really, really big if you want to represent the relation in a matrix. 因为如果要在矩阵中表示关系,则图的结构可能非常大。 The growth of a matrix to represent graphs could run exponentially. 表示图形的矩阵的增长可以成倍地增长。

Works with objects 处理对象

One type of representation that you can do is with java objects. 可以执行的一种表示形式是使用Java对象。 Create two types of objects. 创建两种类型的对象。 One will be the Node and the other will be de edge. 一个将是节点,另一个将是边缘。 The Node object should have one array of edges. Node对象应具有一组边缘。 Like this: (I'm not a java developer so here is in python) 像这样:(我不是Java开发人员,所以这里是python)

class Node:
    def __init__(self, value): #here is the constructor
        self.value = obj #here we are setting the identification of node
        self.edges = []
   def setEdges(self, edge)
        self.edges.append(edge)
   #do other stuffs

The edge will be like this: 边缘将像这样:

class Edge:
    def __init__(self, node1, node2, value):
        self.value = value
        self.nodes = [node1, node2]
        node1.setEdges(self) #self is the same as this in java
        node2.setEdges(self)

What we have here now? 现在我们这里有什么? We have one object that will store the edges that it has, and other object that stores just a value and set his relations. 我们有一个对象将存储其具有的边缘,另一个对象仅存储一个值并设置其关系。 That it works to set the relation. 可以建立关系。 So how to use this in the main script? 那么如何在主脚本中使用它呢?

The main Script 主要脚本

Here is going to be easy. 这将很容易。 Look to an example: 看一个例子:

me = Node('Me')
you = Node('You')

So, how to set the relation? 那么,如何设置关系呢?

Edge(me, you, 'StackOverflow')

This is the edge that explicits the relation of me and you by the StackOverflow. 这是通过StackOverflow显露我与您的关系的优势。 Here you will be able to see in each of objects the array of edges that they have like this: 在这里,您将能够在每个对象中看到它们具有的边缘阵列,如下所示:

me.edges

This will return the array of Edges objects, and with this edge you can find what type of relation you have, and with whom. 这将返回Edges对象的数组,使用该edge,您可以找到所拥有的关系类型以及与谁的关系。

But... How this edge will not be distroyed by the gb? 但是... gb不会破坏这一优势吗? Simple, both Node classes has references to the Edge class. 很简单,两个Node类都引用Edge类。 If none of them has relations to the edge class the gb will consume it. 如果它们都不与边缘类有关系,则gb将消耗它。

By Ref in Java 通过Java中的Ref

I don't remember how set an object by Ref in Java. 我不记得如何在Java中通过Ref设置对象。 I didn't programming in Java since the college. 自大学以来,我没有使用Java编程。 So to this thing works right check the By Ref in java, due to Python byRef is implicit. 因此,要使该方法正常工作,请检查java中的By Ref,因为Python byRef是隐式的。

So with this implementation you can take small arrays, and you can make sure that your nodes and adges will exists with relations. 因此,通过此实现,您可以采用较小的数组,并且可以确保节点和adges将存在关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM