【问题标题】:Implementation of Directed Weighted Graph (Adjacent Matrix)有向加权图(邻接矩阵)的实现
【发布时间】:2020-04-08 06:55:03
【问题描述】:

我需要帮助在 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

【问题讨论】:

  • 为什么removeEdge 需要weight
  • 我被告知要为 add 和 remove 方法加上一个权重,因为它已称重

标签: java data-structures graph adjacency-matrix directed-graph


【解决方案1】:

要删除边缘,您只需将相邻矩阵的单元格更改为 0(它处于默认阶段)。

【讨论】:

  • 所以删除该行将是:相邻矩阵 [来源][目的地] = 0; ?
  • 可以,只要权重始终大于 0。
  • 如果权重总是大于0,则检查矩阵中您要检查边缘是否存在的所需单元格。如果该单元格为 0,则相应节点之间没有边。
【解决方案2】:

你几乎想通了!

假设在您的邻接矩阵中,值为 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] &gt; 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; // if out of range, no edge
    }
}

【讨论】:

    【解决方案3】:

    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;            
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2017-10-13
      • 2014-05-04
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多