Skip to content

Instantly share code, notes, and snippets.

@PrecisoEstudarSempre
Last active May 29, 2017 20:29
Show Gist options
  • Save PrecisoEstudarSempre/f9009aee20dafda9686ec837e14505c2 to your computer and use it in GitHub Desktop.
Save PrecisoEstudarSempre/f9009aee20dafda9686ec837e14505c2 to your computer and use it in GitHub Desktop.
Implementação de um grafo ponderado em Java.
public class WeightedGraph {
private final int MAX_VERTEX = 10;
private Vertex[] vertexs;
private Edge[][] adjacencyMatrix;
private int currentAmountOfVerxtes;
public WeightedGraph(){
this.vertexs = new Vertex[MAX_VERTEX];
adjacencyMatrix = new Edge[MAX_VERTEX][MAX_VERTEX];
currentAmountOfVerxtes = 0;
}
public void addVertex(char label){
vertexs[currentAmountOfVerxtes++] = new Vertex(label);
}
public void addEdgeNonOriented(int start, int end, int weight){
Vertex vertexStart = vertexs[start];
Vertex vertexEnd = vertexs[end];
adjacencyMatrix[start][end] = new Edge(vertexStart, vertexEnd, weight);
adjacencyMatrix[end][start] = new Edge(vertexEnd, vertexStart, weight);
}
public void displayGraph(){
for(int i=0; i<adjacencyMatrix.length; i++){
for(int j=0; j<adjacencyMatrix[i].length; j++){
Edge edge = adjacencyMatrix[i][j];
if(edge != null){
System.out.println(edge.getVertexBegin().getLabel() + " - " + edge.getVertexEnd().getLabel() + ": " + edge.getWeight());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment