Java 有向加权图(邻接矩阵)的实现
我需要帮助实现有向加权图在java中使用邻接矩阵。不知道如何检查是否有连接的边或如何删除,只知道如何添加边Java 有向加权图(邻接矩阵)的实现,java,data-structures,graph,adjacency-matrix,directed-graph,Java,Data Structures,Graph,Adjacency Matrix,Directed Graph,我需要帮助实现有向加权图在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;
// 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
//利用邻接矩阵实现有向加权图
公共类图{
私有整数大小;
私有整数邻接矩阵[];
公共图形(整数大小){
这个。大小=大小;
邻接矩阵=新整数[size][size];
}
公共无效添加(整数源、整数目标、整数权重){
如果(源=0&&destination=0)
邻接矩阵[源][目的]=权重;
}
//需要此函数的帮助,了解如何将第二行设置为等于或新函数
public void removedge(整数源、整数目标、整数权重){
如果(源=0&&destination=0)
邻接矩阵[源][目标]=0;//(不确定)
}
//需要有关整个功能的帮助吗
//用于检查边是否连接的函数
公共布尔isEdge(int源,int目标){
如果(大小>=0&&size=0&&destination
要删除边,只需将相邻矩阵的单元格更改为0(默认阶段为0) 要删除边,只需将相邻矩阵的单元格更改为0(默认阶段为0) 你几乎已经明白了
假设在邻接矩阵中,值为0表示不存在边,值大于0表示存在具有该权重的边
removedge
方法不需要权重,因为它会删除边。此处设置为0是正确的,因为0表示“无边”
你几乎把它弄明白了 假设在邻接矩阵中,值为0表示不存在边,值大于0表示存在具有该权重的边
removedge
方法不需要权重,因为它会删除边。此处设置为0是正确的,因为0表示“无边”
addEdge
中没有重量限制,因此重量可以有任何值,包括0。因此,0不是表示没有边的最佳选择
我建议将权重设置为无穷大。在没有边的地方应用无限权重是有意义的:
邻接矩阵[源][目的]=Integer.MAX\u值代码>
这可能需要在开始时将整个数组邻接矩阵[][]
初始化为整数。最大值
:
for(int[] row : adjacentMatrix)
Arrays.fill(row, Integer.MAX_VALUE);
}
iEdge
也变得简单易读:
public boolean isEdge(int source, int destination) {
if(size >= 0
&& destination >= 0 && destination < size
&& source >= 0 && source < size )
return adjacentMatrix[source][destination] != Integer.MAX_VALUE;
return false;
}
public boolean iEdge(int源,int目标){
如果(大小>=0
&&目的地>=0&&destination=0&&source
在addEdge
中没有重量限制,因此重量可以有任何值,包括0。
因此,0不是表示没有边的最佳选择
我建议将权重设置为无穷大。在没有边的地方应用无限权重是有意义的:
邻接矩阵[源][目的]=Integer.MAX\u值代码>
这可能需要在开始时将整个数组邻接矩阵[][]
初始化为整数。最大值
:
for(int[] row : adjacentMatrix)
Arrays.fill(row, Integer.MAX_VALUE);
}
iEdge
也变得简单易读:
public boolean isEdge(int source, int destination) {
if(size >= 0
&& destination >= 0 && destination < size
&& source >= 0 && source < size )
return adjacentMatrix[source][destination] != Integer.MAX_VALUE;
return false;
}
public boolean iEdge(int源,int目标){
如果(大小>=0
&&目的地>=0&&destination=0&&source
因此,对于删除,该行将是:adjacentMatrix[source][destination]=0?是,前提是权重始终大于0。如果权重始终大于0,则检查矩阵中要检查其边是否存在的所需单元格。如果该单元格为0,则相应节点之间没有边。因此,对于删除,该行将为:adjacentMatrix[source][destination]=0?是,前提是权重始终大于0。如果权重始终大于0,则检查矩阵中要检查其边是否存在的所需单元格。如果该单元格为0,则相应节点之间没有边。removedge
为什么需要一个weight
?我被告知添加和删除方法都要加权,因为它是加权的。请参见:removedge
为什么需要一个weight
?我被告知添加和删除方法都要加权,因为它是加权的加权见: