0% encontró este documento útil (0 votos)
184 vistas3 páginas

Codigo Java

El documento presenta una clase Grafo que implementa un grafo dirigido mediante una matriz de adyacencia. La clase incluye métodos para crear un grafo vacío con un número máximo de vértices y aristas, insertar aristas entre vértices, y mostrar el grafo generado mediante la matriz de adyacencia.

Cargado por

Yake Kohen
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
184 vistas3 páginas

Codigo Java

El documento presenta una clase Grafo que implementa un grafo dirigido mediante una matriz de adyacencia. La clase incluye métodos para crear un grafo vacío con un número máximo de vértices y aristas, insertar aristas entre vértices, y mostrar el grafo generado mediante la matriz de adyacencia.

Cargado por

Yake Kohen
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como TXT, PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 3

System.out.

println("Ingrese la cantidad de aristas");


Scanner leer=new Scanner(System.in);
int NumeroAristas=leer.nextInt();
for(int i=0;i<NumeroAristas;i++){
System.out.println("Ingrese el origen de la arista");
Scanner leeer1= new Scanner(System.in);
int nodoOrigen=leeer1.nextInt();
System.out.println("Ingrese La Llegada");
Scanner leer1= new Scanner(System.in);
int nodoLLegada=leer1.nextInt();
grafo[nodoOrigen][nodoLLegada] = 1;
}
}
----------------------------------------------------------------

/*File: Grafo.java
*Descripcin: Grafo Dirigido implementado con matrices de adyacencia.
*Las aristas no van a tener peso, por lo tanto la matriz es binaria,
*un valor de 1 indica que existe una arista entre dos vertices, y un valor
*de cero indica que no existe una arista entre los vertices.*/
import
import
import
import

java.lang.ArrayIndexOutOfBoundsException;
java.lang.UnsupportedOperationException;
java.lang.RuntimeException;
java.util.Scanner;

public class Grafo {


// private final
int MAX_VERTICES;
// private final
int MAX_ARISTAS;
private int nroAristas;
private int grafo[][];
// Crea un grafo vaco, con un mximo numero de vertices y aristas
public Grafo(int nroVertices, int nroAristas) {
MAX_VERTICES = nroVertices;
MAX_ARISTAS = nroAristas;
this.nroAristas = 0;
grafo = new int[MAX_VERTICES][MAX_VERTICES];
for (int i = 0; i < getMAX_VERTICES(); i++) {
for (int j = 0; j < getMAX_VERTICES(); j++) {
grafo[i][j] = 0;
}
}
}
// Crea un grafo vaco, con un mximo nmero de vertices, y
// vertices al cuadrado como nmero mximo de aristas.
public Grafo(int nroVertices) {
// Llamada al constructor con dos argumentos
this(nroVertices, nroVertices);
}

// Crea un grafo vaco con un mximo nmero de vertices = 5


// y mximo numero de aristas = 25.
public Grafo() {
// Llamada al constructor con dos argumentos
this(5, 25);
}
public int getMAX_VERTICES() {
return MAX_VERTICES;
}
public int getMAX_ARISTAS() {
return MAX_ARISTAS;
}
// Inserta una arista entre dirigida del vertice v1 al vertice v2
public void insertaArista(int Origen, int Direccion)
throws ArrayIndexOutOfBoundsException,
UnsupportedOperationException {
System.out.println("MAX "+getMAX_VERTICES());
if (Origen > getMAX_VERTICES() || Direccion > getMAX_VERTICES()
){
throw new ArrayIndexOutOfBoundsException(
"Vertices invlidos, fuera de rango"
+ "\nRango de vertices:
0 - "
+ (getMAX_VERTICES() - 1
));
} else if (nroAristas == MAX_ARISTAS) {
throw new UnsupportedOperationException(
"No se puede aadir ms aristas");
} else {
grafo[Origen][Direccion] = 1;
}
}
public void mostrarGrafo() {
int[] nodos= new int [MAX_VERTICES];
for(int i=0; i<MAX_VERTICES; i++){
System.out.println("Ingrese los nodos: ");
Scanner leeer = new Scanner(System.in);
int Nodo = leeer.nextInt();
nodos[i]=Nodo;
}
System.out.println("Ingrese la cantidad de aristas");
Scanner leer=new Scanner(System.in);
int NumeroAristas=leer.nextInt();
for(int i=0;i<NumeroAristas;i++){
System.out.println("Ingrese el origen de la arista");
Scanner leeer1= new Scanner(System.in);
int nodoOrigen=leeer1.nextInt();
System.out.println("Ingrese La Llegada");
Scanner leer1= new Scanner(System.in);
int nodoLLegada=leer1.nextInt();
this.insertaArista(nodoOrigen, nodoLLegada);
}

System.out.print("
");
for (int i = 0; i < MAX_VERTICES; i++) {
System.out.printf("%3d ",nodos[i]);
}
System.out.println();
for (int i = 0; i < MAX_VERTICES; i++) {
// System.out.println("Ingrese los Nodos.");
System.out.printf(" %3d", nodos[i]);
for (int j = 0; j < MAX_VERTICES; j++) {
System.out.printf(" %3d", grafo[i][j]);
}
System.out.println();
}
}
}

También podría gustarte