您的位置:首页 > 其它

第四周作业——图的表示

2014-04-03 22:01 344 查看
import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.LinkedList;

public class GraphRepresentation {

    public static void main(String[] args) {

        File file = new File("C:\\tinyG.txt");

        private ArrayList vertexList;

        private int[][] edges;

        private int numOfEdges;

        System.out.println("结点个数是:"+graph.getNumOfVertex());

        System.out.println("边的个数是:"+graph.getNumOfEdges());

        graph.Edge(0, 1);

        System.out.println("删除<V1,V2>边后...");

        System.out.println("结点个数是:"+graph.getNumOfVertex());

        System.out.println("边的个数是:"+graph.getNumOfEdges());

        edges=new int

;

        vertexList=new ArrayList(n);

        numOfEdges=0;

        }

    

    public int getNumOfVertex(){

     return vertexList.size();

    }

    

    public int getNumOfEdges()

    {

        return numOfEdges;

    }

    public Object getValueByIndex(int i)

    {

        return vertexList.get(i);

    }

    public int getWeight(int v1,int v2)

    {

        return edges[v1][v2];

    }

    public void Vertex(Object vertex)

    {

       vertexList.add(vertexList.size(),vertex);

    }

    public void Edge(int v1,int v2,int weight){

        edges[v1][v2]=weight;

        numOfEdges++;

    }

    public void Edge(int v1,int v2)

    {

        edges[v1][v2]=0;

        numOfEdges--;

    }

    public int getFirstNeighbor(int index)

    {

       for(int j=0;j<vertexList.size();j++)

        {

            if (edges[index][j]>0)

            {

                return j;

            }

        }

        return -1;

    }

    public int getNextNeighbor(int v1,int v2)

    {

        for (int j=v2+1;j<vertexList.size();j++)

        {

            if (edges[v1][j]>0)

            {

                return j;

            }

        }

        return -1;

    }

}

        try {

            FileReader fr = new FileReader(file);

            BufferedReader br = new BufferedReader(fr);

            try {

                String s, s2 = new String();

                while ((s = br.readLine()) != null) {

                    s2 += s + "\n ";

                }

                br.close();

                System.out.println(s2);

            } catch (IOException e) {           

                e.printStackTrace();

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: