Last active
May 29, 2017 20:29
-
-
Save PrecisoEstudarSempre/f9009aee20dafda9686ec837e14505c2 to your computer and use it in GitHub Desktop.
Implementação de um grafo ponderado em Java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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