无向图-笔记-代码

//API:
public
interface Graph { int V();//顶点数 int E();//边数 void addEdge(int v, int w);//添加一条边 Iterable<Integer> adj(int v); //某个顶点v的相邻顶点 String toString(); }

用 邻接表数组 实现的图

public class AdjacencyListArrayGraph implements Graph {
    int v;
    int e;

    IntLinked[] adjList;
    //2.优化成 key:顶点 val:相邻定点 的 Hash表(如红黑树,或拉链散列表),但为了集中精力在本质的图的特性上,不用过于麻烦的优化方法。


    public AdjacencyListArrayGraph(int v) {
        this.v = v;
        e = 0;
        adjList = new IntLinked[v];
        for (int i = 0; i < v; i++) {
            adjList[i] = new IntLinked();
        }
    }

    @Override
    public int V() {
        return v;
    }

    @Override
    public int E() {
        return e;
    }

    @Override
    public void addEdge(int v, int w) {
        adjList[v].add(w);
        adjList[w].add(v);
        e++;
    }

    public String toString() {
        String res = "";
        for (int i = 0; i < v; i++) {
            res += "[" + i + "] ";
            Iterable<Integer> it = new ALAGIntIterable(adjList[i].head);
            Iterator<Integer> itr = it.iterator();
            while (itr.hasNext()) {
                int to = itr.next();
                res += to + " ";
            }
            res += "\n";
        }
        return res;
    }

    @Override
    public Iterable<Integer> adj(int v) {
        return new ALAGIntIterable(adjList[v].head);
    }

    public class ALAGIntIterable implements Iterable<Integer> {
        IntNode node;
        public ALAGIntIterable(IntNode node) {
            this.node = node;
        }

        @Override
        public Iterator<Integer> iterator() {
            return new IntNodeIterator(node);
        }

        @Override
        public void forEach(Consumer<? super Integer> action) {

        }

        @Override
        public Spliterator<Integer> spliterator() {
            return null;
        }

        public class IntNodeIterator implements Iterator<Integer> {
            IntNode node;
            public IntNodeIterator(IntNode node){
                this.node = node;
            }
            @Override
            public boolean hasNext() {
                return node != null;
            }

            @Override
            public Integer next() {
                IntNode tmp = node;
                node = node.n;
                return tmp.k;
            }
        }
    }

    public static class ALAGBuilder {
        File f;

        public ALAGBuilder(File f) {
            this.f = f;
        }

        public AdjacencyListArrayGraph build() {
            FileInputStream fis = null;
            BufferedReader br = null;
            AdjacencyListArrayGraph g = null;
            int v = 0, e = 0;
            try {
                fis = new FileInputStream(f);
                br = new BufferedReader(new InputStreamReader(fis));
                String txt = null;

                txt = br.readLine();
                if (txt != null) {
                    v = Integer.parseInt(txt);
                }
                txt = br.readLine();
                if (txt != null) {
                    e = Integer.parseInt(txt);
                }
                g = new AdjacencyListArrayGraph(v);
                int count = 0;
                while ((txt = br.readLine()) != null) {
                    String[] line = txt.split(" ");
                    int from = Integer.parseInt(line[0]);
                    int to = Integer.parseInt(line[1]);
                    g.addEdge(from, to);
                }
                return g;
            } catch (Exception e1) {
                e1.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            return null;
        }
    }

    public static void main(String[] args) {
        Graph g = new AdjacencyListArrayGraph(10);

        File f = new File("D:\\aa.txt");
        Graph g2 = new ALAGBuilder(f).build();
        System.out.println(g2.toString());
    System.out.println("degree 0 : " + GraphUtil.degree(g2, 0));
    System.out.println("maxDegree : " + GraphUtil.maxDegree(g2));
    System.out.println("avgDegree : " + GraphUtil.avgDegree(g2));
    System.out.println("numberOfSelfloops : " + GraphUtil.numberOfSelfloops(g2));
}

aa.txt 的文件格式:

顶点数量
边数量
顶点1 顶点2
。。。
。。。
顶点x 顶点y

头2行是一行一个单独的数字

后面是记录 边的信息的,一行一条边

aa.txt

5
8
0 1
0 2
1 3
1 4
2 3
2 4
0 0
1 1

一些工具类

public class GraphUtil {
    //
    public static int degree(Graph g, int v) {
        Iterable<Integer> it = g.adj(v);
        int count = 0;
        for (int n : it) {
            count++;
        }
        return count;
    }

    //所有定点最大度
    public static int maxDegree(Graph g) {
        int max = 0;
        for (int v = 0; v < g.V(); v++) {
            int d = degree(g, v);
            max = max < d ? d : max;
        }
        return max;
    }

    //所有定点平均度数
    public static double avgDegree(Graph g) {
        return 2.0 * g.E() / g.V();  // * 2 因为 一条边 被算了2次(出现在一对定点的各自的相邻顶点列表)
    }

    //图中自环的个数
    public static int numberOfSelfloops(Graph g) {
        int count = 0;
        for (int v = 0; v < g.V(); v++)
            for (int w : g.adj(v))
                if (v == w) count++;

        return count / 2;  //因为addEdge 的实现,一个自环会被加2次到同一个定点上,所以除以2
    }
}

结果:

[0] 1 2 0 0 
[1] 0 3 4 1 1 
[2] 0 3 4 
[3] 1 2 
[4] 1 2 

degree 0 : 4
maxDegree : 5
avgDegree : 3.2
numberOfSelfloops : 2

图是一种数据结构,一些内容来自数学的图论研究成果。

1.有/无向 (方向,单向)

2。有/无权(权重)

可组合出4种图,无向图,有向图,有权图,有向有权图

这里的例子是无向图

常见概念有:

顶点,边,度,自环,连通图,平行边,稠密/稀疏图,子图,树,森林,无环图,二分图,连通子图,生成树森林等(挺多的)

连通图:是一个整体,图中任意一点可以到达这个图上其他任意点

生成树:只是连通图的一副子图

生成树森林:一个图的所有连通子图的生成树集合

最后抱怨一下(你要接受我的精神污染,你生存的意义在于被别人压榨):

感觉自己已经失去独立思考,自主自考,自我意识了。 思考能力。。。。

基本都是 copy别人的东西,自己也不太动脑子了,疯狂看别人的想法,实现,思想, 自己也不加联想和批判。 一股脑的 你说有什么优势 ,那我就 ‘嗯!’ 有什么优势,记也记不住什么东西。如同机械般的‘思考’ 和 ‘记忆’

 

猜你喜欢

转载自www.cnblogs.com/cyy12/p/11588477.html
今日推荐