英文:
Implementation of Directed Weighted Graph (Adjacent Matrix)
问题
// Implementation of directed weighted Graph using Adjacency 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;
}
public void removeEdge(int source, int destination) {
if (source < size && source >= 0 && destination < size && destination >= 0)
adjacentMatrix[source][destination] = 0;
}
public boolean isEdge(int source, int destination) {
if (source >= 0 && source < size && destination >= 0 && destination < size) {
if (adjacentMatrix[source][destination] != 0)
return true;
else
return false;
}
return false; // Added a default return for cases when conditions are not met
}
}
Note: I've corrected the syntax of the code and added a default return in the isEdge
function for cases where the input conditions are not met. If you have any further questions or need more assistance, feel free to ask.
英文:
I need help implementing directed weighted graph in java using adjacency matrix. Not sure how to check if there are connected edges or how to remove, only know how to add edges.
// 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
答案1
得分: 0
你可以通过将邻接矩阵中对应的单元格改为0来删除边缘(在默认阶段时它就是0)。
英文:
To remove edge you can just change that cell of the adjacent matrix to 0 (which it was at the default stage).
答案2
得分: 0
你已经几乎弄清楚了!
假设在你的邻接矩阵中,值为0意味着没有边,而大于0的值意味着存在具有该权重的边。
removeEdge
方法不需要 weight
,因为它用于移除一条边。在这里将值设为0是正确的,因为0表示“无边”。
public void removeEdge(int source, int destination) {
if (source < size && source >= 0 && destination < size && destination >= 0)
adjacentMatrix[source][destination] = 0;
}
由于你被“告知”在那里放一个 weight
参数,可能的原因之一是,你可能只应该在权重与传入的权重匹配时才删除该边?
isEdge
方法应该检查 adjacentMatrix[source][destination] > 0
,而不是 adjacentMatrix[source][destination] == 1
,因为任何正值都表示“有一条边”。
public boolean isEdge(int source, int destination) {
if (size >= 0 && size < size && destination >= 0 && destination < size) {
return adjacentMatrix[source][destination] > 0;
} else {
return false; // 如果超出范围,则无边
}
}
英文:
You've almost figured it out!
Assuming that in your adjacency matrix, a value of 0 means there is no edge, and a value greater than 0 means there is an edge with that weight.
The removeEdge
method does not need a weight
, since it removes an edge. Setting to 0 is correct here, as 0 means "no edge".
public void removeEdge (int source, int destination) {
if (source < size && source >= 0 && destination < size && destination >= 0)
adjacentMatrix [source][destination] = 0;
}
}
Since you were told to put a weight
parameter there, one possibly could be that you are supposed to only remove the edge if the weight matches the passed in weight?
The isEdge
method should check adjacentMatrix[source][destination] > 0
instead of adjacentMatrix[source][destination] == 1
, since any positive value means "there's an edge there".
public boolean isEdge(int source, int destination) {
if(size >= 0 && size < size && destination >= 0 && destination < size) {
return adjacentMatrix[source][destination] > 0;
} else {
return false; // if out of range, no edge
}
}
答案3
得分: 0
在 addEdge
中对于权重没有限制,所以权重可以是任何值,包括0。
所以0不是指示没有边的最佳选择。
我建议将权重设置为无穷大。在没有边的情况下应用无穷大权重是有意义的:
adjacentMatrix[source][destination] = Integer.MAX_VALUE;
这可能需要在开始时将整个数组 adjacentMatrix[][]
初始化为 Integer.MAX_VALUE
:
for (int[] row : adjacentMatrix)
Arrays.fill(row, Integer.MAX_VALUE);
}
isEdge
也变得简单和可读:
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;
}
英文:
There is no limitation on weight in addEdge
so weight can have any value, including 0.<br/>
So 0 is not your best choice for indicating that there is no edge. <br>
I would recommend setting the weight to infinite one. It makes sense to apply infinite weight where there is no edge:
adjacentMatrix [source][destination] =Integer.MAX_VALUE;
This may require initializing the entire array adjacentMatrix[][]
to Integer.MAX_VALUE
at start:
for(int[] row : adjacentMatrix)
Arrays.fill(row, Integer.MAX_VALUE);
}
isEdge
also become simple and readable:
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;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论