Skip to content

Commit 0878bc6

Browse files
committed
Add - Implementation Dijkstra in Java
1 parent 0c7068a commit 0878bc6

File tree

9 files changed

+785
-0
lines changed

9 files changed

+785
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
package computationalRepresentation;
3+
4+
/**
5+
*
6+
* @author BRUNO S LIMA
7+
*/
8+
public class Adjacencia {
9+
10+
private int numeroVertices;
11+
private RepresentacaoComputacional representacao;
12+
private boolean tipoGrafo;
13+
14+
public Adjacencia(int numeroVertices, RepresentacaoComputacional representacao, boolean tipoGrafo){
15+
16+
this.setNumeroVertices(numeroVertices);
17+
this.representacao = representacao;
18+
this.setTipoGrafo(tipoGrafo);
19+
}
20+
21+
public int getNumeroVertices() {
22+
return numeroVertices;
23+
}
24+
25+
public void setNumeroVertices(int numeroVertices) {
26+
this.numeroVertices = numeroVertices;
27+
}
28+
29+
public void adicionaAresta(int i, int j, int peso){
30+
31+
this.representacao.adicionaAresta(i, j, peso);
32+
}
33+
34+
public boolean verificaAdjacencia(int i, int j){
35+
36+
return(this.representacao.verificarAdjacencia(i, j));
37+
}
38+
39+
public int getPeso(int i, int j){
40+
41+
return(this.representacao.getPeso(i, j));
42+
}
43+
44+
public void exibir(){
45+
46+
System.out.println("Número de vertices: " + this.numeroVertices);
47+
this.representacao.exibir();
48+
}
49+
50+
public boolean hasNext(int pos){
51+
52+
return(this.representacao.hasNext(pos));
53+
}
54+
55+
public int next(int pos){
56+
57+
return(this.representacao.next(pos));
58+
}
59+
60+
public void inicializaIterator(int pos){
61+
62+
this.representacao.inicializaIterator(pos);
63+
}
64+
65+
public boolean isTipoGrafo() {
66+
return tipoGrafo;
67+
}
68+
69+
public void setTipoGrafo(boolean tipoGrafo) {
70+
this.tipoGrafo = tipoGrafo;
71+
}
72+
73+
74+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
package computationalRepresentation;
3+
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.util.Scanner;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
9+
import javax.swing.JOptionPane;
10+
11+
/**
12+
*
13+
* @author BRUNO S LIMA
14+
*/
15+
public class Arquivo {
16+
17+
public Arquivo(){
18+
19+
}
20+
21+
public static Adjacencia leituraArquivo(String nomeArquivo, boolean tipoRepresentacao){
22+
23+
Adjacencia adjacencia = null;
24+
25+
try {
26+
27+
Scanner scan = new Scanner(new FileReader(nomeArquivo));
28+
29+
//Lendo e verificando se é grafo ou digrafo
30+
boolean tipoGrafo;
31+
if(scan.nextInt() == 0) tipoGrafo = false; //Falso para grafo
32+
else tipoGrafo = true; //Verdadeiro para Digrafo
33+
34+
int numeroVertices = scan.nextInt(); //Lendo número de vertices
35+
36+
//Instanciando a estrutura que será utilizada.
37+
MatrizAdjacencia ma;
38+
ListaAdjacencia la;
39+
if(tipoRepresentacao) {
40+
41+
ma = new MatrizAdjacencia(numeroVertices);
42+
adjacencia = new Adjacencia(numeroVertices, ma, tipoGrafo);
43+
}
44+
else {
45+
46+
la = new ListaAdjacencia(numeroVertices);
47+
adjacencia = new Adjacencia(numeroVertices, la, tipoGrafo);
48+
}
49+
50+
int i, j, peso;
51+
52+
while(scan.hasNext()){
53+
54+
i = scan.nextInt(); //Lendo vertice 1
55+
j = scan.nextInt(); //Lendo vertice 2
56+
peso = scan.nextInt(); //Lendo peso (ou valor)
57+
58+
if(tipoGrafo == true) adjacencia.adicionaAresta(i, j, peso); //Para Digrafos
59+
else{
60+
61+
//Para Grafos
62+
adjacencia.adicionaAresta(i,j, peso);
63+
adjacencia.adicionaAresta(j, i, peso);
64+
}
65+
66+
67+
}
68+
69+
scan.close();
70+
71+
} catch (FileNotFoundException ex) {
72+
Logger.getLogger(Arquivo.class.getName()).log(Level.SEVERE, null, ex);
73+
JOptionPane.showMessageDialog(null, "Erro na abertura do arquivo!");
74+
}
75+
76+
return(adjacencia);
77+
78+
}
79+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
package computationalRepresentation;
3+
4+
import java.util.Iterator;
5+
import javax.swing.JOptionPane;
6+
7+
/**
8+
*
9+
* @author BRUNO S LIMA
10+
*/
11+
public class Lista /*implements Iterable<No>*/{
12+
13+
public No primeiro;
14+
public No ultimo;
15+
public int tamanhoLista;
16+
public No posicaoAtual = null;
17+
18+
/*
19+
@Override
20+
public Iterator<No> iterator() {
21+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
22+
}
23+
24+
public class ListaIterator implements Iterator<No>{
25+
26+
public No aux;
27+
28+
ListaIterator(){
29+
this.aux = primeiro;
30+
}
31+
32+
@Override
33+
public boolean hasNext() {
34+
35+
if(aux != null) return(true);
36+
return(false);
37+
}
38+
39+
@Override
40+
public No next() {
41+
42+
No proximo = aux;
43+
aux = aux.getProx();
44+
return(proximo);
45+
}
46+
47+
}*/
48+
49+
public Lista(){
50+
51+
this.primeiro = null;
52+
this.ultimo = null;
53+
this.tamanhoLista = 0;
54+
}
55+
56+
public int getTamanhoLista(){
57+
58+
return(this.tamanhoLista);
59+
}
60+
61+
public boolean estaVazia(){
62+
63+
if(this.getTamanhoLista() == 0) return(true);
64+
return(false);
65+
}
66+
67+
public void inserirNo(int valor, int peso){
68+
69+
No no = new No(valor, peso);
70+
71+
if(this.estaVazia() == true){
72+
73+
this.primeiro = this.ultimo = no;
74+
}
75+
else{
76+
77+
this.ultimo.setProx(no);
78+
this.ultimo = no;
79+
}
80+
81+
this.tamanhoLista++;
82+
}
83+
84+
public void removerNo(No no){
85+
86+
No atual, anterior;
87+
atual = anterior = this.primeiro;
88+
int cont = 1;
89+
90+
if(this.estaVazia() == false){
91+
92+
while(cont <= this.getTamanhoLista() && atual.getValor() != no.getValor()){
93+
94+
anterior = atual;
95+
atual = atual.getProx();
96+
cont++;
97+
}
98+
99+
if(atual.getValor() == no.getValor()){
100+
101+
if(this.getTamanhoLista() == 1){
102+
103+
this.primeiro = this.ultimo = null;
104+
}
105+
else{
106+
if(atual == this.primeiro){
107+
this.primeiro = this.primeiro.getProx();
108+
}
109+
else{
110+
anterior.setProx(atual.getProx());
111+
}
112+
}
113+
114+
this.tamanhoLista--;
115+
}
116+
}
117+
else JOptionPane.showMessageDialog(null, "A lista está vazia!");
118+
119+
}
120+
121+
public void exibir(){
122+
123+
No temp = this.primeiro;
124+
String valores = "";
125+
int cont = 1;
126+
127+
if(this.estaVazia() == false){
128+
129+
while(cont <= this.getTamanhoLista()){
130+
131+
valores += " " + Integer.toString(temp.getValor()) + " |";
132+
temp = temp.getProx();
133+
cont++;
134+
}
135+
136+
System.out.print(valores);
137+
}
138+
139+
}
140+
141+
142+
public boolean contem(int valor){
143+
144+
No aux = this.primeiro;
145+
146+
while (aux != null){
147+
148+
if (aux.getValor() == valor) return(true);
149+
aux = aux.getProx();
150+
151+
}
152+
return(false);
153+
}
154+
155+
}

0 commit comments

Comments
 (0)