Java 使用文本文件数据的符号定向图

Java 使用文本文件数据的符号定向图,java,algorithm,std,directed-graph,Java,Algorithm,Std,Directed Graph,我太困了,非常感谢你的帮助。我目前正在学习算法,但我不知道从哪里开始 我最近收到了一段代码(我们只是真正地做了理论工作,所以看到代码让我吓了一跳),我的任务是修改这段代码,从文本文件中获取细节,并将其放在图表中。文本文件与此类似 Trout is-a fish Fish has gills Fish has fins Fish is food Fish is-an animal 里面还有很多。我只是想知道。我将如何开始这整件事?我有一百万个问题要问,但我觉得只要我知道如何使用文本文件指定顶点,

我太困了,非常感谢你的帮助。我目前正在学习算法,但我不知道从哪里开始

我最近收到了一段代码(我们只是真正地做了理论工作,所以看到代码让我吓了一跳),我的任务是修改这段代码,从文本文件中获取细节,并将其放在图表中。文本文件与此类似

Trout is-a fish
Fish has gills
Fish has fins
Fish is food
Fish is-an animal
里面还有很多。我只是想知道。我将如何开始这整件事?我有一百万个问题要问,但我觉得只要我知道如何使用文本文件指定顶点,我就可以解决这些问题?下面是我提供并必须编辑的代码。任何帮助都是很好的,只要朝着正确的方向努力就行了

(另外,在addEdge类中,权重是什么?我知道它是遍历边的“成本”,但如何分配权重?)

谢谢

public class Graph {
    private final int MAX_VERTS = 20;
    private final int INFINITY = 1000000;
    private Vertex vertexList[]; // list of vertices
    private int adjMat[][]; // adjacency matrix
    private int nVerts; // current number of vertices
    private int nTree; // number of verts in tree
    private DistPar sPath[]; // array for shortest-path data
    private int currentVert; // current vertex
    private int startToCurrent; // distance to currentVert
// -------------------------------------------------------------
    public Graph() // constructor
    {
    vertexList = new Vertex[MAX_VERTS];
    // adjacency matrix
    adjMat = new int[MAX_VERTS][MAX_VERTS];
    nVerts = 0;
    nTree = 0;
    for(int j=0; j<MAX_VERTS; j++) // set adjacency
        for(int k=0; k<MAX_VERTS; k++) // matrix
            adjMat[j][k] = INFINITY; // to infinity
    sPath = new DistPar[MAX_VERTS]; // shortest paths
    } // end constructor
// -------------------------------------------------------------
    public void addVertex(char lab)
    {
    vertexList[nVerts++] = new Vertex(lab);
    }
// -------------------------------------------------------------
    public void addEdge(int start, int end, int weight)
    {
    adjMat[start][end] = weight; // (directed)
    }
// -------------------------------------------------------------
    public void path() // find all shortest paths
    {
    int startTree = 0; // start at vertex 0
    vertexList[startTree].isInTree = true;
    nTree = 1; // put it in tree
    // transfer row of distances from adjMat to sPath
    for(int j=0; j<nVerts; j++)
    {
        int tempDist = adjMat[startTree][j];
        sPath[j] = new DistPar(startTree, tempDist);
    }
    // until all vertices are in the tree
    while(nTree < nVerts)
    {
        int indexMin = getMin(); // get minimum from sPath
        int minDist = sPath[indexMin].distance;
        if(minDist == INFINITY) // if all infinite
        { // or in tree,
            System.out.println("There are unreachable vertices");
            break; // sPath is complete
        }
        else
        { // reset currentVert
            currentVert = indexMin; // to closest vert
            startToCurrent = sPath[indexMin].distance;
            // minimum distance from startTree is
            // to currentVert, and is startToCurrent
        }
        // put current vertex in tree
        vertexList[currentVert].isInTree = true;
        nTree++;
        adjust_sPath(); // update sPath[] array
   } // end while(nTree<nVerts)
    displayPaths(); // display sPath[] contents
    nTree = 0; // clear tree
    for(int j=0; j<nVerts; j++)
        vertexList[j].isInTree = false;
    } // end path()
// -------------------------------------------------------------
    public int getMin() // get entry from sPath
    { // with minimum distance
    int minDist = INFINITY; // assume minimum
    int indexMin = 0;
    for(int j=1; j<nVerts; j++) // for each vertex,
    { // if it’s in tree and
        if( !vertexList[j].isInTree && // smaller than old one
            sPath[j].distance < minDist )
        {
            minDist = sPath[j].distance;
            indexMin = j; // update minimum
        }
    } // end for
    return indexMin; // return index of minimum
    } // end getMin()
// -------------------------------------------------------------
    public void adjust_sPath()
    {
    // adjust values in shortest-path array sPath
    int column = 1; // skip starting vertex
    while(column < nVerts) // go across columns
    {
    // if this column’s vertex already in tree, skip it
    if( vertexList[column].isInTree )
    {
        column++;
        continue;
    }
    // calculate distance for one sPath entry
    // get edge from currentVert to column
    int currentToFringe = adjMat[currentVert][column];
    // add distance from start
    int startToFringe = startToCurrent + currentToFringe;
    // get distance of current sPath entry
    int sPathDist = sPath[column].distance;
    // compare distance from start with sPath entry
    if(startToFringe < sPathDist) // if shorter,
    { // update sPath
        sPath[column].parentVert = currentVert;
        sPath[column].distance = startToFringe;
    }
    column++;
    } // end while(column < nVerts)
    } // end adjust_sPath()
// -------------------------------------------------------------
    public void displayPaths()
    {
        for(int j=0; j<nVerts; j++) // display contents of sPath[]
    {
        System.out.print(vertexList[j].label + "="); // B=
        if(sPath[j].distance == INFINITY)
            System.out.print("inf"); // inf
        else
            System.out.print(sPath[j].distance); // 50
        char parent = vertexList[ sPath[j].parentVert ].label;
        System.out.print("(" + parent + ") "); // (A)
    }
        System.out.println("");
    }
// -------------------------------------------------------------
} // end class Graph
公共类图{
私人最终整数最大值=20;
私有最终整数无穷大=1000000;
私有顶点顶点列表[];//顶点列表
私有整数邻接矩阵[][];//邻接矩阵
private int nVerts;//当前顶点数
private int nTree;//树中的顶点数
私有DistPar sPath[];//最短路径数据的数组
私有int currentVert;//当前顶点
private int startToCurrent;//到currentVert的距离
// -------------------------------------------------------------
公共图()//构造函数
{
vertexList=新顶点[最大顶点];
//邻接矩阵
adjMat=新整数[MAX_VERTS][MAX_VERTS];
nVerts=0;
nTree=0;

对于(int j=0;j我绘制图形的方式是,我有一个边列表或数组,而不是将该信息存储在矩阵中。我将创建一个包含两个节点的内部边类,因为这是一个方向图,两个节点必须彼此不同。您也可以使用边类而不是DistPar类来跟踪短边st path。(或者您可以重新调整distPar类的用途,以实现边缘功能)

权重是赋予边缘的属性。我喜欢使用的类比是航线。假设有一条从纽约到洛杉矶的航线,但在那架飞机上买一张票需要300美元,但是,如果你选择一条通过连接机场的航线,票只需要150美元。在这种情况下,你可以将每个机场视为一个节点,然后e机场之间的路线是连接节点的边缘。在这种情况下,节点的“重量”就是价格。如果您希望以尽可能低的成本从纽约到洛杉矶,您会选择更便宜的路线,即使它经过更多的机场

权重基本上将任意两个节点之间最短路径的定义从连接节点的最少数量转移到这两个节点之间的最小权重。Dijkstra的算法与您实现的算法类似,但也利用了权重,如上文所述重新定义了最短路径


我希望这是有帮助的!

只有“是a”关系才能在图中生成边,不是吗?另一方面,节点应该包含“有a”项。类似这样的内容:鳟鱼——是a->鱼(有金枪鱼,有鳍)--是->动物--是->食物。但是,必须注意最后两项。鱼是动物,鱼是食物实际上看起来像多重继承,考虑到您使用的是Java,这有点讽刺。我已经理解了图形的外观,我只是不知道如何将其转换为Java,如果这有意义的话sense?Ignore“has a”行,直到“is a”关系生效。您需要一个数据结构来将数字映射到名称,反之亦然(最原始的示例:使用type
String[MAX\u VERTS]
)。然后每当您看到“isa”行时,就像“鳟鱼是鱼”一样,检查鳟鱼和鱼是否有相应的编号,如果没有添加,则
add_edge(numberforrout,numberForFish,1)
。边缘权重在我看来应该都是1。我愿意帮助你,但这个问题似乎并不笼统,对除你之外的任何人都没有帮助。所以我的问题是:你还需要帮助吗?如果需要,我会添加一个答案,否则如果没有人需要,我宁愿不要浪费时间。