File: test_boundary.py

package info (click to toggle)
python-networkx 1.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,052 kB
  • ctags: 3,986
  • sloc: python: 52,132; makefile: 176
file content (104 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
from nose.tools import *
import networkx as nx
from networkx import convert_node_labels_to_integers as cnlti

class TestBoundary:

    def setUp(self):
        self.null=nx.null_graph()
        self.P10=cnlti(nx.path_graph(10),first_label=1)
        self.K10=cnlti(nx.complete_graph(10),first_label=1)

    def test_null_node_boundary(self):
        """null graph has empty node boundaries"""
        null=self.null
        assert_equal(nx.node_boundary(null,[]),[])
        assert_equal(nx.node_boundary(null,[],[]),[])
        assert_equal(nx.node_boundary(null,[1,2,3]),[])
        assert_equal(nx.node_boundary(null,[1,2,3],[4,5,6]),[])
        assert_equal(nx.node_boundary(null,[1,2,3],[3,4,5]),[])

    def test_null_edge_boundary(self):
        """null graph has empty edge boundaries"""
        null=self.null
        assert_equal(nx.edge_boundary(null,[]),[])
        assert_equal(nx.edge_boundary(null,[],[]),[])
        assert_equal(nx.edge_boundary(null,[1,2,3]),[])
        assert_equal(nx.edge_boundary(null,[1,2,3],[4,5,6]),[])
        assert_equal(nx.edge_boundary(null,[1,2,3],[3,4,5]),[])

    def test_path_node_boundary(self):
        """Check node boundaries in path graph."""
        P10=self.P10
        assert_equal(nx.node_boundary(P10,[]),[])
        assert_equal(nx.node_boundary(P10,[],[]),[])
        assert_equal(nx.node_boundary(P10,[1,2,3]),[4])
        assert_equal(sorted(nx.node_boundary(P10,[4,5,6])),[3, 7])
        assert_equal(sorted(nx.node_boundary(P10,[3,4,5,6,7])),[2, 8])
        assert_equal(nx.node_boundary(P10,[8,9,10]),[7])
        assert_equal(sorted(nx.node_boundary(P10,[4,5,6],[9,10])),[])

    def test_path_edge_boundary(self):
        """Check edge boundaries in path graph."""
        P10=self.P10

        assert_equal(nx.edge_boundary(P10,[]),[])
        assert_equal(nx.edge_boundary(P10,[],[]),[])
        assert_equal(nx.edge_boundary(P10,[1,2,3]),[(3, 4)])
        assert_equal(sorted(nx.edge_boundary(P10,[4,5,6])),[(4, 3), (6, 7)])
        assert_equal(sorted(nx.edge_boundary(P10,[3,4,5,6,7])),[(3, 2), (7, 8)])
        assert_equal(nx.edge_boundary(P10,[8,9,10]),[(8, 7)])
        assert_equal(sorted(nx.edge_boundary(P10,[4,5,6],[9,10])),[])
        assert_equal(nx.edge_boundary(P10,[1,2,3],[3,4,5]) ,[(2, 3), (3, 4)])


    def test_k10_node_boundary(self):
        """Check node boundaries in K10"""
        K10=self.K10

        assert_equal(nx.node_boundary(K10,[]),[])
        assert_equal(nx.node_boundary(K10,[],[]),[])
        assert_equal(sorted(nx.node_boundary(K10,[1,2,3])),
                     [4, 5, 6, 7, 8, 9, 10])
        assert_equal(sorted(nx.node_boundary(K10,[4,5,6])),
                     [1, 2, 3, 7, 8, 9, 10])
        assert_equal(sorted(nx.node_boundary(K10,[3,4,5,6,7])),
                            [1, 2, 8, 9, 10])
        assert_equal(nx.node_boundary(K10,[4,5,6],[]),[])
        assert_equal(nx.node_boundary(K10,K10),[])
        assert_equal(nx.node_boundary(K10,[1,2,3],[3,4,5]),[4, 5])

    def test_k10_edge_boundary(self):
        """Check edge boundaries in K10"""
        K10=self.K10

        assert_equal(nx.edge_boundary(K10,[]),[])
        assert_equal(nx.edge_boundary(K10,[],[]),[])
        assert_equal(len(nx.edge_boundary(K10,[1,2,3])),21)
        assert_equal(len(nx.edge_boundary(K10,[4,5,6,7])),24)
        assert_equal(len(nx.edge_boundary(K10,[3,4,5,6,7])),25)
        assert_equal(len(nx.edge_boundary(K10,[8,9,10])),21)
        assert_equal(sorted(nx.edge_boundary(K10,[4,5,6],[9,10])),
                     [(4, 9), (4, 10), (5, 9), (5, 10), (6, 9), (6, 10)])
        assert_equal(nx.edge_boundary(K10,[1,2,3],[3,4,5]),
                     [(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), 
                      (2, 5), (3, 4), (3, 5)])


    def test_petersen(self):
        """Check boundaries in the petersen graph

        cheeger(G,k)=min(|bdy(S)|/|S| for |S|=k, 0<k<=|V(G)|/2)
        """
        from random import sample
        P=nx.petersen_graph()
        def cheeger(G,k):
            return min([float(len(nx.node_boundary(G,sample(G.nodes(),k))))/k 
                        for n in range(100)])

        assert_almost_equals(cheeger(P,1),3.00,places=2)
        assert_almost_equals(cheeger(P,2),2.00,places=2)
        assert_almost_equals(cheeger(P,3),1.67,places=2)
        assert_almost_equals(cheeger(P,4),1.00,places=2)
        assert_almost_equals(cheeger(P,5),0.80,places=2)