Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package lab8;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Hubert
- */
- public class Lab8 implements IWGraph{
- int [][] lista;
- public static void main(String[] args) throws Exception {
- Lab8 l =new Lab8();
- l.addVertices(7);
- l.addEdge(1,2,2);l.addEdge(1,3,4);
- l.addEdge(2,5,4);
- l.addEdge(3,5,3);l.addEdge(3,4,2);
- l.addEdge(4,1,1);l.addEdge(4,6,2);
- l.addEdge(6,2,3);l.addEdge(6,5,1);
- l.writeGraph();
- l.ford(1);
- }
- @Override
- public void addVertices(int i) throws Exception {
- lista =new int [i][i];
- for(int a=0;a<i;a++)
- for(int j=0;j<i;j++)
- lista[a][j]=INFTY;
- }
- @Override
- public void addEdge(int start, int stop, int weight) throws Exception {
- lista[start][stop]=weight;
- }
- @Override
- public int wCheck(int start, int stop) throws Exception {
- return lista[start][stop];
- }
- @Override
- public void writeGraph() {
- for(int i=0;i<lista.length;i++){
- System.out.println("Wartosci dla "+i);
- for(int j=0;j<lista.length;j++)
- {
- try {
- if(wCheck(i,j)!=INFTY)
- System.out.print(j+" ");
- }
- catch (Exception ex) {
- Logger.getLogger(Lab8.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- System.out.println();
- }
- }
- @Override
- public void ford(int start) throws Exception {
- int [] Drogi =new int[lista.length];
- int [] cofanie =new int[lista.length];
- for(int i=0;i<lista.length;i++)
- {
- Drogi[i]=wCheck(start,i);
- cofanie[i]=-1;
- }
- Drogi[start]=0;
- for(int a=0;a<lista.length;a++){
- for(int i=0;i<lista.length;i++){
- for(int y=0;y<lista.length;y++){
- if(wCheck(i, y) != INFTY){
- if(Drogi[y]>(Drogi[i]+wCheck(i,y)) ){
- Drogi[y]=Drogi[i]+wCheck(i,y);
- cofanie[y]=i;
- }//if 1
- }//if2
- }//y
- }//i
- }//
- for(int i=0;i<lista.length;i++)
- {
- System.out.println("Dla "+i+" dlugosc : "+Drogi[i]);
- int cofanie_pomoc = cofanie[i];
- System.out.println("poprzednik "+i);
- while(cofanie_pomoc != start){
- if(cofanie_pomoc != -1){
- System.out.println("poprzednik "+cofanie_pomoc);
- cofanie_pomoc = cofanie[cofanie_pomoc];
- }
- else cofanie_pomoc = start;
- }
- System.out.println("poprzednik "+start);
- }
- }
- @Override
- public void dijkstra(int start) throws Exception {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void SSS() {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement