Esempio n. 1
0
class SymbolDigraph:
    def __init__(self, file_name, seperator):
        # First pass reading the file
        self._hash_table = LPHashTable(
        )  # This hash table is used to convert from key to vertex index
        self._graph = None  # The actual underlying directed graph

        file = open(file_name, "r")
        count = 0
        lines = file.read().splitlines()
        for line in lines:
            data = line.split(seperator)
            # Continuously adding keys to the hash table along with the corresponding index
            for key in data:
                if not self._hash_table.contains(key):
                    self._hash_table.put(key, count)
                    count += 1

        self._keys = np.empty(
            count, dtype=object)  # This array to convert index to key
        for name in self._hash_table.keys():
            self._keys[self._hash_table.get(name)] = name

        file.close()

        # Second pass, Build the actual graph after knowing exactly how many vertices we will need
        second_pass = open(file_name)
        self._graph = DirectedGraph(count)
        lines = second_pass.read().splitlines()
        for line in lines:
            data = line.split(seperator)
            start = self._hash_table.get(data[0])
            for i in range(1, len(data)):
                self._graph.add_edge(start, self._hash_table.get(data[i]))

        second_pass.close()

    # Returns the actual graph
    def graph(self):
        return self._graph

    # Checks if the graph contains the given key
    def contains(self, key):
        return self._hash_table.contains(key)

    # Returns the index of the given key
    def index_of(self, key):
        return self._hash_table.get(key)

    # Returns the key associated with the given index
    def name_of(self, index):
        return self._keys[index]
class TestKnots(unittest.TestCase):
    def setUp(self):
        self.v = Vertex('v')
        self.w = Vertex('w')
        self.x = Vertex('x')
        self.e1 = Arc(self.v, self.w)
        self.e2 = Arc(self.w, self.x)
        self.e3 = Arc(self.x, self.v)
        
        self.dg = DirectedGraph([self.v, self.w, self.x], 
            [self.e1, self.e2, self.e3])
        
    def test_positive(self):
        
        #triangular knot
        self.assertEqual(self.dg.has_knot(), True)
  
        #triangular knot w/ an edge into it
        self.a = Vertex('a')
        self.dg.add_vertex(self.a)
        self.Kanye = Arc(self.a,self.v)
        self.dg.add_edge(self.Kanye)
        self.assertTrue(self.dg.has_knot())
    
    def test_ngon_with_chord(self):
        v1 = Vertex('v1')
        v2 = Vertex('v2')
        v3 = Vertex('v3')
        v4 = Vertex('v4')
        v5 = Vertex('v5')
        e1 = Arc(v2,v1)
        e2 = Arc(v3,v2)
        e3 = Arc(v3,v4)
        e4 = Arc(v4,v5)
        e5 = Arc(v5,v1)
        
        ngon = DirectedGraph([v1,v2,v3,v4,v5],
            [e1,e2,e3,e4,e5])
        
        self.assertFalse(ngon.has_knot())
        
        chord = Arc(v1,v4)
        ngon.add_edge(chord)

        self.assertTrue(ngon.has_knot())
    def test_ngon_with_chord(self):
        v1 = Vertex('v1')
        v2 = Vertex('v2')
        v3 = Vertex('v3')
        v4 = Vertex('v4')
        v5 = Vertex('v5')
        e1 = Arc(v2,v1)
        e2 = Arc(v3,v2)
        e3 = Arc(v3,v4)
        e4 = Arc(v4,v5)
        e5 = Arc(v5,v1)
        
        ngon = DirectedGraph([v1,v2,v3,v4,v5],
            [e1,e2,e3,e4,e5])
        
        self.assertFalse(ngon.has_knot())
        
        chord = Arc(v1,v4)
        ngon.add_edge(chord)

        self.assertTrue(ngon.has_knot())
Esempio n. 4
0
        self.visited = [False] * len(graph)
        transpose_graph = [None] * len(graph)

        for vertex in range(len(graph)):
            if not self.visited[vertex]:
                self.transpose_util(vertex, transpose_graph)
        return transpose_graph

    @staticmethod
    def display_graph(graph):
        for v in range(len(graph)):
            print(v, " --> ", end=" ")
            if graph[v] is not None:
                for adj_v in graph[v]:
                    print(adj_v, end=" ")
            print()


g = DirectedGraph(6)
g.add_edge(5, 2)
g.add_edge(5, 0)
g.add_edge(4, 0)
g.add_edge(4, 1)
g.add_edge(2, 3)
g.add_edge(3, 1)
TransposeGraph().display_graph(g.get_graph())
print("Transposed ---> ")
obj = TransposeGraph()
trans = obj.transpose(g.get_graph())
obj.display_graph(trans)
Esempio n. 5
0
        while queue:
            v = queue.pop(0)
            if graph[v]:
                for adj_v in graph[v]:
                    if color[adj_v] == color[v]:
                        return False
                    if color[adj_v] is None:
                        queue.append(adj_v)
                        color[
                            adj_v] = "red" if color[v] == "black" else "black"

        return True


g = DirectedGraph(6)
g.add_edge(0, 2)
g.add_edge(2, 0)
g.add_edge(1, 2)
g.add_edge(0, 3)
g.add_edge(0, 4)
g.add_edge(0, 5)
g.add_edge(4, 5)
# g.add_edge(2, 3)
obj = BipartiteGraph()
# print(obj.is_bipartite(g.get_graph(), 0))

lst = [[2, 4], [2, 3, 4], [0, 1], [1], [0, 1], [7],
       [9], [5], [], [6], [12, 14], [], [10], [], [10], [19], [18], [], [16],
       [15], [23], [23], [], [20, 21], [], [], [27], [26], [], [], [34],
       [33, 34], [], [31], [30, 31], [38, 39], [37, 38, 39], [36], [35, 36],
       [35, 36], [43], [], [], [40], [], [49], [47, 48, 49], [46, 48, 49],