コード例 #1
0
ファイル: test_scc.py プロジェクト: songy23/neu-cs5800
 def testGeneralGraph(self):
     g = {
         0: [],
         1: [0, 4],
         2: [1, 5],
         3: [1],
         4: [1],
         5: [2],
         6: [4, 7],
         7: [5, 9],
         8: [6],
         9: [8]
     }
     cc = scc.scc(g)
     # 0 itself forms a cc.
     # 3 itself forms a cc.
     # 1 and 4 form a cc.
     # 2 and 5 form a cc.
     # 6, 7, 8, and 9 form a cc.
     self.assertEqual(cc[1], cc[4])
     self.assertEqual(cc[2], cc[5])
     self.assertEqual(cc[6], cc[7])
     self.assertEqual(cc[6], cc[8])
     self.assertEqual(cc[6], cc[9])
     # Check different cc are numbered differently.
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[6])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[1], cc[3])
     self.assertNotEqual(cc[1], cc[6])
     self.assertNotEqual(cc[2], cc[3])
     self.assertNotEqual(cc[2], cc[6])
     self.assertNotEqual(cc[3], cc[6])
コード例 #2
0
ファイル: test_scc.py プロジェクト: saikoa0709/neu-cs5800
 def testGeneralGraph(self):
     g = {0:[],
          1:[0,4],
          2:[1,5],
          3:[1],
          4:[1],
          5:[2],
          6:[4,7],
          7:[5,9],
          8:[6],
          9:[8]}
     cc = scc.scc(g)
     # 0 itself forms a cc.
     # 3 itself forms a cc.
     # 1 and 4 form a cc.
     # 2 and 5 form a cc.
     # 6, 7, 8, and 9 form a cc.
     self.assertEqual(cc[1], cc[4])
     self.assertEqual(cc[2], cc[5])
     self.assertEqual(cc[6], cc[7])
     self.assertEqual(cc[6], cc[8])
     self.assertEqual(cc[6], cc[9])
     # Check different cc are numbered differently.
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[6])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[1], cc[3])
     self.assertNotEqual(cc[1], cc[6])
     self.assertNotEqual(cc[2], cc[3])
     self.assertNotEqual(cc[2], cc[6])
     self.assertNotEqual(cc[3], cc[6])
コード例 #3
0
ファイル: test_scc.py プロジェクト: songy23/neu-cs5800
 def testNoEdgeGraph(self):
     g = {0: [], 1: [], 2: [], 3: [], 4: []}
     cc = scc.scc(g)
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[4])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[3], cc[4])
コード例 #4
0
ファイル: test_scc.py プロジェクト: songy23/neu-cs5800
 def testTreeGraph(self):
     g = {0: [1, 2], 1: [3, 4], 2: [5], 3: [], 4: [], 5: []}
     cc = scc.scc(g)
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[4])
     self.assertNotEqual(cc[0], cc[5])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[3], cc[4])
 def find_cycles(self):
     self.init()
     self.g = Graph(self.graph, self.nodes)
     self.list_scc = scc(self.g)
     self.list_scc = [scc for scc in self.list_scc
                      if len(scc) > 1]  # remove single node scc
     if len(self.list_scc) == 0:
         return False
     else:
         # print(self.list_scc)
         return True
コード例 #6
0
def getRanking(results, names, pvalues):
    #print(results)
    l = len(results)
    edges, vertices = getEdges(results, True, pvalues)
    #print(edges)
    #strip edges
    edgesXY = []
    for e in edges:
        edgesXY.append((e[0], e[1]))
    sccs = scc.scc(edgesXY)
    #print(sccs)
    ranking = getBest(results, sccs, names)
    return ranking
コード例 #7
0
ファイル: test_scc.py プロジェクト: saikoa0709/neu-cs5800
 def testNoEdgeGraph(self):
     g = {0:[],
          1:[],
          2:[],
          3:[],
          4:[]}
     cc = scc.scc(g)
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[4])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[3], cc[4])
コード例 #8
0
ファイル: hw4.py プロジェクト: dimfox/coursera-algo1
def main():
    print '%s: reading input' % (datetime.datetime.now())
    g = read_input('SCC.txt')
    nodes = set()
    for s in sorted(g.keys()):
        nodes.add(s)
        for t in g[s]:
            nodes.add(t)
    print '%s: Sloving' % (datetime.datetime.now())
    groups = scc(g)
    print '%s: Done Sloving' % (datetime.datetime.now())
    print 'total_nodes in groups: %s' % sum(count_len(groups))
    print 'total_nodes in graph: %s' % len(list(nodes))
    print '%s: sorting to get output' % (datetime.datetime.now())
    print count_len(groups)[:20]
コード例 #9
0
ファイル: test_scc.py プロジェクト: saikoa0709/neu-cs5800
 def testTreeGraph(self):
     g = {0:[1,2],
          1:[3,4],
          2:[5],
          3:[],
          4:[],
          5:[]}
     cc = scc.scc(g)
     self.assertNotEqual(cc[0], cc[1])
     self.assertNotEqual(cc[0], cc[2])
     self.assertNotEqual(cc[0], cc[3])
     self.assertNotEqual(cc[0], cc[4])
     self.assertNotEqual(cc[0], cc[5])
     self.assertNotEqual(cc[1], cc[2])
     self.assertNotEqual(cc[3], cc[4])
コード例 #10
0
def two_sat(graph):
    """
    Given a 2SAT problem represented by a directed graph in forms of 
    {tail:[head_list], ...}, compute the satisfiability.

    
    Below symbol 'v' means 'or', '^' means 'and', '<=>' means 'equivalent to',
    '-' means 'negate', and '->' means material implication.

    A 2SAT problem in CNF, which is (x1 v x2)^(x1 v x3)^(x2 v x3)..., can be
    represented by an implication graph by replacing each of its disjunctions
    by a pair of material implications. It is satisfiable if and only if no
    vertex and its negation belong to the same SCC (strongly connected component)
    of the implication graph. SCC can be computed in linear time O(m + n), where
    m is the number of the edges of the graph and n is the number of nodes.

    Equivalence between boolean clause and material implication:
    (x1 v x2) <=> (-x1 -> x2) <=> (-x2 -> x1)

    An implication graph is constructed by adding edges (-x1, x2) and (-x2, x1)
    for each disjunction (x1 v x2). Despite the equivalence between the boolean 
    clause and EACH material implication, BOTH edges must be added to the graph
    in order for the edges to be able to represent the relation of vertices.

    If x and -x are both in the same SCC, which implies (-x -> x) ^ (x -> -x),
    then a contradiction is concluded as below:

    (-x -> x) <=> (x v x) <=> x
    (x -> -x) <=> (-x v -x) <=> -x
    thus, (-x v x) ^ (x -> -x) <=> (x ^ -x) <=> False

    https://class.coursera.org/algo2-002/forum/thread?thread_id=428#post-1573
    https://class.coursera.org/algo2-002/forum/thread?thread_id=428#comment-1584
    """

    groups = scc.scc(graph)
    for group in groups.values():
        group = set(group)
        for node in group:
            if -node in group:
                return 0
    return 1
コード例 #11
0
def scc_2sat(size, m):
    """
    AvB = True as: (not A => B) or  (not B => A) as graph edges
    """
    graph = {k:[] for k in range(-size, size+1)}
    graph.pop(0)

    for a,b in m:
        graph[-a].append(b)
        graph[-b].append(a)

    # determine scc, if in single scc both a and not a -> return false

    components = scc(graph)

    # check in each component a and not a
    for comp in components:
        vertices = set(comp)
        for v in vertices:
            if -v in vertices:
                return False
    return True
コード例 #12
0
ファイル: test.py プロジェクト: SHLo/algorithms
    def test_sample_1(self):
        graph = parse_file("sample-1.txt")
        result = scc(graph)
        answer = [3, 3, 3]

        self.assertEqual(result, answer)
コード例 #13
0
ファイル: test.py プロジェクト: SHLo/algorithms
    def test_sample_5(self):
        graph = parse_file("sample-5.txt")
        result = scc(graph)
        answer = [6, 3, 2, 1]

        self.assertEqual(result, answer)
コード例 #14
0
ファイル: msx.py プロジェクト: xresende/pymsx
if not options.bb_file:
    print('No BIOS/BASIC ROM selected (e.g. msxbiosbasic.rom)')
    sys.exit(1)

# bb == bios/basic
bb = rom(options.bb_file, debug, 0x0000)
put_page(0, 0, 0, bb)
put_page(0, 0, 1, bb)

snd = sound(debug)

if options.scc_rom:
    for o in options.scc_rom:
        parts = o.split(':')
        scc_obj = scc(parts[2], snd, debug)
        scc_slot = int(parts[0])
        scc_subslot = int(parts[1])
        put_page(scc_slot, scc_subslot, 1, scc_obj)
        put_page(scc_slot, scc_subslot, 2, scc_obj)

if options.disk_rom:
    for o in options.disk_rom:
        parts = o.split(':')
        disk_slot = int(parts[0])
        disk_subslot = int(parts[1])
        disk_obj = disk(parts[2], debug, parts[3])
        put_page(disk_slot, disk_subslot, 1, disk_obj)

if options.rom:
    for o in options.rom:
コード例 #15
0
ファイル: scctest.py プロジェクト: swuiojkl/CMPUT204
from scc import scc
from reverse import reverse

bookfigure3point7 =  {  'A': ['B', 'C', 'F'],
                        'B': ['E'],
                        'C': ['D'],
                        'D': ['A', 'H'],
                        'E': ['F', 'G', 'H'],
                        'F': ['B', 'G'],
                        'G': [],
                        'H': ['G']
                     }

print "\nscc for textbook figure 3.7:"
scc(bookfigure3point7)

bookfigure3point8 =  {  'A': ['C'],
                        'B': ['A', 'D'],
                        'C': ['E', 'F'],
                        'D': ['C'],
                        'E': [],
                        'F': []
                     }

print "\nscc for textbook figure 3.8:"
scc(bookfigure3point8)

bookfigure3point9 =  {  'A': ['B'],
                        'B': ['C','D','E'],
                        'C': ['F'],
                        'D': [],