1
votes

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
3
Why does removeEdge need a weight?Sweeper
i was told to put a weight for both add and remove method since its weighedbilly parker

3 Answers

0
votes

To remove edge you can just change that cell of the adjacent matrix to 0 (which it was at the default stage).

0
votes

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
    }
}
0
votes

There is no limitation on weight in addEdge so weight can have any value, including 0.
So 0 is not your best choice for indicating that there is no edge.
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;            
}