有向加权图的实现(相邻矩阵)

问题描述 投票:0回答:1

我需要使用邻接矩阵在Java中实现有向加权图的帮助。不确定如何检查是否有连接的边或如何删除,仅知道如何添加边。

// Implementation of directed weighted Graph using Adjacent Matrix

public class Graph {
    private int size;
    private int adjacentMatrix[][];


public Graph (int size) {
    this.size = size;
    adjacentMatrix = new int [size][size];
}


public void addEdge (int source, int destination, int weight) {
    if (source < size && source >= 0 && destination < size && destination >= 0)
        adjacentMatrix [source][destination] = weight;
    }

// need help in this function for what to set second line equal to or new function in general
public void removeEdge (int source, int destination, int weight) {
    if (source < size && source >= 0 && destination < size && destination >= 0)
        adjacentMatrix [source][destination] = 0;  //(not sure)
    }


//need help with this entire function
//function to check if edges are connected
public boolean isEdge(int source, int destination) {
    if(size >= 0 && size < size && destination >= 0 && destination < size) {
        if(adjacentMatrix[source][destination] == 1)
            return true;
        else
            return false;
     }
  }
 }   
}

 // I'm not sure if I did this correct at all any help would be appreciated
java data-structures graph adjacency-matrix directed-graph
1个回答
0
投票

要删除边缘,您只需将相邻矩阵的该单元格更改为0(默认情况下为0)。

© www.soinside.com 2019 - 2024. All rights reserved.