Undirected graph - Notes - Code

The API //: 
public
interface Graph { int V (); // number of vertices int E (); // number of edges void addEdge ( int V, int W); // add an edge of the Iterable <Integer> ADJ ( int V) ; // adjacent vertices of a vertex v String toString (); }

 

Achieved using adjacency table array in FIG.

 

public  class AdjacencyListArrayGraph the implements Graph {
     int V;
     int E; 

    IntLinked [] adjList; 
    // 2. optimized to key: vertex val: Hash table adjacent to the point (e.g., red-black trees, hash tables, or zipper), but in order to concentrate in view of the nature of the characteristics, do not be too cumbersome optimization method. 


    public AdjacencyListArrayGraph ( int V) {
         the this .v = V; 
        E = 0 ; 
        adjList = new new IntLinked [V];
         for ( int I = 0; I <V; I ++ ) { 
            adjList [I] = new 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 file formats:

The number of vertices 
number of edges 
vertex vertex 2 
. . . 
. . . 
X y vertices vertices

Line 2 is the first line of a single number

Followed by the information recording side, an edge line

 

aa.txt

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

 

 

Some tools

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; 
    } 

    // All average power point 
    public  static  Double avgDegree (Graph G) {
         return 2.0 * of gE () / of gV ();   // * 2 calculations because an edge is twice (each occurrence a pair of adjacent vertices point list) 
    } 

    // number of rings from FIG 
    public  static  int numberOfSelfloops (Graph G) {
         int COUNT 0 = ;
         for ( int V = 0; V <of gV (); V ++ )
             for (int W: g.adj (V))
                 IF (V == W) COUNT ++ ; 

        return COUNT / 2;   // because the implementation of addEdge, a loopback is applied twice to the same point, so divided by 2 
    } 
}

 

result:

[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

 

Figure is a data structure, some of the content from the mathematics of graph theory research.

From

1. Yes / No (the direction, unidirectional)

2. Yes / no right (weight)

FIG 4 kinds may be combined, undirected graph, a directed graph, the right graph, entitled to FIG.

Here is an example undirected graph

Common concepts include:

Vertices, edges, degree, loopback, FIG communication, parallel sides, dense / sparse graph, the subgraph, trees, forests, acyclic graph, bipartite graph, connected subgraph, spanning tree forests (a lot of)

 

FIG communication: as a whole, can reach any point in the figure this figure any other point

Spanning Tree: just a connectivity graph subgraph

Spanning Tree Forest: all connected subgraph a set of spanning tree of FIG.

 

 

Finally complain about (you have to accept my spiritual pollution, meaning you survive is to squeeze by others):

I feel that they have lost independent thinking, self-tests and self-conscious. Thinking ability. . . .

Basically copy other people's things, and he is not the mindless, crazy to see what others think, realize, thinking that he did not add the association and criticism. What you say will fall into the advantage, then I 'ah! 'What are the advantages, remember not remember anything. As a mechanical-like 'thinking' and 'memory'

 

Guess you like

Origin www.cnblogs.com/cyy12/p/11588477.html