Está en la página 1de 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