Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total

Camino mas corto

Ejemplo en java del algoritmo para hallar el camino mas corto de un grafo. se implementa el algoritmo de floyd

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public final class Grafo {

	private int nnodos;

	private int nodos[][][];

	private char nombres[];

	Grafo(int n) {
		this.nnodos = n;
		this.nodos = new int[nnodos][nnodos][2];
		this.nombres = new char[nnodos];
	}

	public void ingresarArco(int n1, int n2, int peso) {
		this.nodos[n1][n2][0] = peso;
		this.nodos[n2][n1][0] = peso;
		this.nodos[n1][n2][1] = n1;
		this.nodos[n2][n1][1] = n2;
	}

	public void ingresarNombre(int nodo, char letra) {
		this.nombres[nodo] = letra;
	}

	public void calcular() {
		int i, j, k;
		for (i = 0; i < this.nnodos; i++) {
			for (j = 0; j < this.nnodos; j++) {
				for (k = 0; k < this.nnodos; k++) {
					if (this.nodos[i][k][0] + this.nodos[k][j][0] < this.nodos[i][j][0]) {
						this.nodos[i][j][0] = this.nodos[i][k][0]
								+ this.nodos[k][j][0];
						this.nodos[i][j][1] = k;
					}
				}
			}
		}
	}

	public int pesominimo(int org, int des) {
		return this.nodos[org][des][0];
	}

	public String caminocorto(int org, int des) {
		String cam;
		if (org == des) {
			cam = "->" + nombres[org];
		} else {
			cam = caminocorto(org, this.nodos[org][des][1]) + "->"
					+ nombres[des];
		}
		return cam;
	}

	public char getNombre(int nodo) {
		return this.nombres[nodo];
	}

	public static void main(String args[]) throws IOException {
		Grafo g;

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String temp;
		int res;

		System.out.println("Entre el numero de nodos del grafo:\n");
		temp = br.readLine();
		res = Integer.parseInt(temp);

		g = new Grafo(res);

		for (int i = 0; i < res; i++) {
			System.out.println("Cual es el nombre del nodo [" + (i + 1)
					+ "]:\n");
			temp = br.readLine();
			g.ingresarNombre(i, temp.charAt(0));
		}
		for (int i = 0; i < res; i++) {
			for (int j = 0; j < res; j++) {
				if (i < j) {
					System.out.println("El nodo " + g.getNombre(i)
							+ " esta conectado con el nodo " + g.getNombre(j)
							+ " (s/n)\n");
					temp = br.readLine();
					if (temp.charAt(0) == 's') {
						int peso;
						System.out.println("Cual es el peso del arco:\n");
						temp = br.readLine();
						peso = Integer.parseInt(temp);
						g.ingresarArco(i, j, peso);
					} else {
						g.ingresarArco(i, j, 10000);
					}
				}
			}
		}

		g.calcular();
		for (int i = 0; i < res; i++) {
			for (int j = 0; j < res; j++) {
				if (i > j) {
					System.out.println("El camino mas corto entre los nodos:"
							+ g.getNombre(i) + "-" + g.getNombre(j) + " es: \n"
							+ g.caminocorto(i, j) + " y su peso es: "
							+ g.pesominimo(i, j));
				}
			}
		}
	}
}

Plotting graph on pys60

Here's the first step towards porting matplotlib to pys60.
I try to replicate some easy examples in
matplotlib tutorial.

First, we need a float range function.
from __future__ import generators

def arange(start, stop=None, step=None):
    if stop is None:
        stop = float(start)
        start = 0.0
    if step is None:
        step = 1.0
    cur = float(start)
    while cur < stop:
        yield cur
        cur += step

Then, a function to draw the axes and ticks.
In the future, this should be called automatically from plot().
from appuifw import *
app.body = canvas = Canvas()
width, height = canvas.size

def axes(xyrange, position=[18, height-11, width-10, 10], formatter=lambda x:x):
    global left, bottom, right, top, min_x, min_y, scale_x, scale_y
    left, bottom, right, top = position
    min_x, max_x, step_x, min_y, max_y, step_y = xyrange
    scale_x = float(right-left)/(max_x-min_x)
    scale_y = float(bottom-top)/(max_y-min_y)
    canvas.clear()
    canvas.rectangle([(left,top), (right+1, bottom+1)], 0)
    for x in arange(min_x, max_x, step_x):
        canvas.text((14+scale_x*(x-min_x), height-1), unicode(formatter(x)))
        canvas.point((left+scale_x*(x-min_x), bottom-1), 0)
        canvas.point((left+scale_x*(x-min_x), top+1), 0)
    for y in arange(min_y, max_y, step_y):
        canvas.text((2, bottom+2-scale_y*(y-min_y)), unicode(formatter(y)))
        canvas.point((left+1, bottom-scale_y*(y-min_y)), 0)
        canvas.point((right-1, bottom-scale_y*(y-min_y)), 0)

And lastly, the plot function. Now it has only a few features.
More will be added depending on what is needed.
def plot(xs, ys=None):
    if ys==None:
        ys = xs
        xs = range(len(ys))
    last = left+(xs[0]-min_x)*scale_x, bottom-(ys[0]-min_y)*scale_y
    for i in range(1, len(ys)):
        p = left+(xs[i]-min_x)*scale_x, bottom-(ys[i]-min_y)*scale_y
        canvas.line([last, p], 0x00000ff)
        last = p
    canvas.point(last, 0x0000ff)

When we want to plot a graph, we called both axes() and plot()
# a straight line
>>> axes([0,3.1,.5, 1,4.1,.5])
>>> plot([1,2,3,4])

# a parabola y = x^2
>>> axes([1,4.1,1, 0,16.1,2], formatter=int)
>>> plot([1,2,3,4], [1,4,9,16])

See the sreenshot of the first.

« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total