コード例 #1
0
def main():
    inf = float("inf")
    gmat7 = [[0, 0, 1, 0, 0, 0, 0, 1, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0],
             [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1],
             [0, 0, 0, 0, 0, 0, 1, 0, 0]]

    gmat8 = [[inf, 6, 4, 5, inf, inf, inf, inf, inf],
             [inf, inf, inf, inf, 1, inf, inf, inf, inf],
             [inf, inf, inf, inf, 1, inf, inf, inf, inf],
             [inf, inf, inf, inf, inf, 2, inf, inf, inf],
             [inf, inf, inf, inf, inf, inf, 9, 7, inf],
             [inf, inf, inf, inf, inf, inf, inf, 4, inf],
             [inf, inf, inf, inf, inf, inf, inf, inf, 2],
             [inf, inf, inf, inf, inf, inf, inf, inf, 4],
             [inf, inf, inf, inf, inf, inf, inf, inf, inf]]

    g = GraphAL(gmat7)
    ts = toposort(g)
    print(ts)

    g8 = GraphAL(gmat8, inf)
    cpath = critical_path(g8)
    print(cpath)
コード例 #2
0
def main():
    inf = float("inf")
    gmat5 = [[0, 10, inf, inf, 19, 21],
             [10, 0, 5, 6, inf, 11],
             [inf, 5, 0, 6, inf, inf],
             [inf, 6, 6, 0, 18, 14],
             [19, inf, inf, 18, 0, 7],
             [21, 11, inf, 14, 7, 0]]
    g5 = GraphAL(gmat5, inf)
    spt = Kruskal(g5)
    prim_spt = Prim(g5)
    print(spt)
    print(prim_spt)
コード例 #3
0
def main():
    gmat4 = [[0, 50, 10, inf, 45, inf],
             [inf, 0, 15, inf, 5, inf],
             [20, inf, 0, 15, inf, inf],
             [inf, 16, inf, 0, 35, inf],
             [inf, inf, inf, 30, 0, inf],
             [inf, inf, inf, 3, inf, 0]]
    g = GraphAL(gmat4, inf)
    paths0 = dijkstra(g, 0)
    paths1 = dijkstra(g, 1)
    print("start v0:", paths0)
    print("start v1:", paths1)
    # 打印路径
    #path(g, v0, v1)
    print("-" * 20)
    paths = floyd(g)
    print(paths[0])
    print(paths[1])
コード例 #4
0
class TestGraph(unittest.TestCase):
    g = None
    data = {
        "A": {"B": 5, "C": 1},
        "B": {"A": 5, "C": 2, "D": 1},
        "C": {"A": 1, "B": 2, "D": 4, "E": 8},
        "D": {"B": 1, "C": 4, "E": 3, "F": 6},
        "E": {"C": 8, "D": 3},
        "F": {"D": 6},
    }

    # 连通网
    data2 = {
        "V1": {"V2": 6, "V3": 1, "V4": 5},
        "V2": {"V1": 6, "V3": 5, "V5": 3},
        "V3": {"V1": 1, "V2": 5, "V4": 5, "V5": 6, "V6": 4},
        "V4": {"V1": 5, "V3": 5, "V6": 2},
        "V5": {"V2": 3, "V3": 6, "V6": 6},
        "V6": {"V3": 4, "V4": 2, "V5": 6},
    }

    # 连通网
    data3 = {
        "a": {"c": 11, "d": 5, "b": 5},
        "b": {"a": 5, "d": 3, "g": 7, "e": 9},
        "c": {"a": 11, "d": 7, "f": 6},
        "d": {"a": 5, "b": 3, "c": 7, "g": 20},
        "e": {"b": 9, "g": 8},
        "f": {"c": 6, "g": 8},
        "g": {"f": 8, "d": 20, "b": 7, "e": 8},

    }

    # 连通网
    data4 = {
        'a': {'b': 19, 'e': 14, 'g': 18},
        'b': {'a': 19, 'e': 12, 'd': 7, 'c': 5},
        'c': {'b': 5, 'd': 3},
        'd': {'c': 5, 'b': 7, 'e': 8, 'f': 21},
        'e': {'a': 14, 'b': 12, 'd': 8, 'g': 16},
        'f': {'d': 21, 'g': 27},
        'g': {'a': 18, 'e': 16, 'f': 27}
    }

    # 有向网AOE
    data5 = {
        'V0': {'V1': 6, 'V2': 4, 'V3': 5},
        'V1': {'V4': 1},
        'V2': {'V4': 1},
        'V3': {'V5': 2},
        'V4': {'V6': 9, 'V7': 7},
        'V5': {'V7': 4},
        'V6': {'V8': 2},
        'V7': {'V8': 4},
        'V8': {}
    }

    # 有向回环图
    data6 = {
        'V0': {'V1': 0},
        'V1': {'V2': 0},
        'V2': {'V3': 0},
        'V3': {'V1': 0},
    }

    # 有向图AOV
    data7 = {
        'C1': {'C4': 0, 'C2': 0, 'C12': 0},
        'C2': {'C3': 0},
        'C3': {'C5': 0, 'C7': 0, 'C8': 0},
        'C4': {'C5': 0},
        'C5': {'C7': 0},
        'C6': {'C8': 0},
        'C7': {},
        'C8': {},
        'C9': {'C10': 0, 'C11': 0, 'C12': 0},
        'C10': {'C12': 0},
        'C11': {'C6': 0},
        'C12': {},
    }

    courseData = {
        'C1': '程序设计基础',
        'C2': '离散数学',
        'C3': '数据结构',
        'C4': '汇编语言',
        'C5': '高级语言程序设计',
        'C6': '计算机原理',
        'C7': '编译原理',
        'C8': '操作系统',
        'C9': '高度数学',
        'C10': '线性代数',
        'C11': '普通物理',
        'C12': '数值分析'
    }

    g = graph.GraphAL(graph=data)
    print("图的结构为:")
    print(g)

    @classmethod
    def setUpClass(cls):
        print("图的结构为:")
        print(TestGraph.g)
        pass

    def setUp(self):
        print()

    def tearDown(self):
        print("------------------------测试完成-----------------------------------\n")

    def test_dfs(self):
        print("dfs测试:")
        dfs, dfsparent = TestGraph.g.dfs("A")
        print("DFS:" + graph.GraphAL.printPath(dfs))
        print("DFS生成路径:" + dfsparent.__str__())
        print("DFS生成路径打印:" + graph.GraphAL.printTreePath(dfsparent).__str__())
        pass

    def test_bfs(self):
        print("bfs测试:")
        bfs, bfsparent = TestGraph.g.bfs("A")
        print("BFS:" + graph.GraphAL.printPath(bfs))
        print("BFS生成路径:" + bfsparent.__str__())
        print("BFS生成路径打印:" + graph.GraphAL.printTreePath(bfsparent).__str__())
        pass

    def test_dijkstra(self):
        print("迪杰斯特拉测试:")
        TestGraph.g.printMinPath("A", "E")
        distance, parent = TestGraph.g.dijkstra('A')
        print("各个顶点到A的最短路径打印:" + graph.GraphAL.printTreePath(parent).__str__())
        print("距离为:\n" + distance.__str__())
        pass

    def test_prim(self):
        print("测试普里姆算法")
        TestGraph.g._graph = TestGraph.data2
        result1 = TestGraph.g.prim('V1')
        # TestGraph.g._graph = TestGraph.data3
        # result2 = TestGraph.g.prim('a')
        # TestGraph.g._graph = TestGraph.data4
        # result3 = TestGraph.g.prim('a')
        print(result1)
        # print(result2)
        # print(result3)

    def test_kruskal(self):
        print("测试克鲁斯卡尔算法")
        TestGraph.g._graph = TestGraph.data2
        result1 = TestGraph.g.kruskal()
        print(result1)
        # TestGraph.g._graph = TestGraph.data3
        # result2 = TestGraph.g.kruskal()
        # print(result2)
        # TestGraph.g._graph = TestGraph.data4
        # result3 = TestGraph.g.kruskal()
        # print(result3)
        # TestGraph.g._graph = TestGraph.data
        pass

    def test_topological_sort(self):
        print("拓扑排序测试")
        gd = graphdi.GraphDI(TestGraph.data5)
        result = gd.topological_sort()
        print(result)

        gd = graphdi.GraphDI(TestGraph.data6)
        result = gd.topological_sort()
        if not result:
            print("有向环,不能使用拓扑排序")
        else:
            print(result)

        gd = graphdi.GraphDI(TestGraph.data7)
        result = gd.topological_sort()
        courseResult = []
        for key in result:
            courseResult.append(TestGraph.courseData[key])
        print(courseResult)

    def test_criticalPath(self):
        print("关键路径测试")
        gd = graphdi.GraphDI(TestGraph.data5)
        print(gd)
        result, cps = gd.criticalPath()
        for key in result.keys():
            print('事件 %s : 最早开始时间:%s;最晚开始时间:%s' % (key, result[key][0], result[key][1]))
        for cp in cps:
            print('关键路径:%s -> %s' % (cp[0], cp[1]))
        pass

    def test_normal(self):
        print("常规测试")
        vertexNum = TestGraph.g.get_vertexNum()
        print(TestGraph.g.isConnect('A', 'B'))
        # print(TestGraph.g._graph['A']['B'])
        # print(vertexNum)

    def test_digraph(self):
        print("有向图常规测试")
        gd = graphdi.GraphDI(TestGraph.data5)
        print(gd)
        print("顶点个数:" + gd.get_vertexNum().__str__())
        print("V4-V6的边:" + gd.get_edge('V4', 'V6').__str__())
        print("V1的出边:" + gd.get_outEdge('V1').__str__())
        print("V4的入边:" + gd.get_inEdge('V4').__str__())
        print("V0的入边:" + gd.get_inEdge('V0').__str__())
コード例 #5
0
from prioQueue_heap import PrioQueue
from graph import GraphAL


def dijkstra_shortest_paths(graph, v0):
    vnum = graph.vertex_num()
    assert 0 <= v0 < vnum
    paths = [None] * vnum
    count = 0
    # (p, v, v')表示从v0经v到v'的已知最短路径的长度为p
    cands = PrioQueue([(0, v0, v0)])  # 初始队列
    while count < vnum and not cands.is_empty():
        plen, u, vmin = cands.dequeue()  # 取路径最短顶点
        if paths[vmin]:
            continue
        paths[vmin] = (u, plen)  # u为v0到vmin最短路径的前一节点,plen为最短路径距离
        for v, w in graph.out_edges(vmin):
            if not paths[v]:
                cands.enqueue((plen + w, vmin, v))
        count += 1
    return paths


if __name__ == '__main__':
    mat = [[0, 0, 5, 2, 0, 0, 0], [11, 0, 4, 0, 0, 4, 0],
           [0, 3, 0, 0, 2, 0, 0], [0, 0, 0, 0, 6, 0, 0], [0, 0, 0, 0, 0, 0, 2],
           [0, 0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0, 0, 0]]
    g = GraphAL(mat)
    print(dijkstra_shortest_paths(g, 0))
コード例 #6
0
    """递归构造DFS生成树"""

    vnum = graph.vertex_num()
    span_forest = [None] * vnum

    def dfs(graph, v):  # 递归遍历函数,在递归中记录经由边
        nonlocal span_forest  # 需要修改非局部变量span_forest, 声明为nonlocal
        for u, weight in graph.out_edges(v):
            if span_forest[u] is None:
                span_forest[u] = (v, weight)  # v到u的边,权重为weight
                dfs(graph, u)

    for vi in range(vnum):
        if span_forest[vi] is None:
            span_forest[vi] = (vi, 0)
            dfs(graph, vi)
    return span_forest


if __name__ == '__main__':
    gmat1 = [[0, 1, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0],
             [1, 0, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 0, 0, 1],
             [0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0]]
    g1 = GraphAL(gmat1, 0)
    dfs1 = DFS_graph(g1, 0)
    print(list(dfs1))

    dfs_tree = DFS_span_forest(g1)
    print(dfs_tree)
コード例 #7
0
 # SIMULATED ANNEALING
 print(dt.datetime.now())
 alpha_list = [.99, .9, .8]
 density_list = [.1, .7]
 header = 'Num_G_nodes, Avg_time, Max_cost, Avg_cost, Netx_avg_cost, Percent_MIS, Density'
 num_runs = 5
 X_avg = 0
 for a in alpha_list:
     fp = open(f'./Data/SA_data_a_{a}.txt', 'w')
     fp.write(f'{header}\n')
     for dp in density_list:
         for G_nodes in range(15, 200):
             numerator = 0
             edges = math.floor((G_nodes * (G_nodes - 1) / 2) * dp)
             e = nx.dense_gnm_random_graph(G_nodes, edges)
             G = GraphAL(G_nodes, e.edges)
             X_avg = 0
             max_K_set = None
             max_sol_cost = float("-inf")
             avg_cost = 0
             MIS_avg = 0
             for _ in range(num_runs):
                 X = nx.maximal_independent_set(e)
                 X_avg += len(X)
                 K = Subgraph(G, random_subset=True)
                 start = time.time()
                 K_sol, K_sol_cost = simulated_annealing(
                     G, a, ITR_PER_T, MAX_ITR, FREEZE)
                 avg_cost += K_sol_cost
                 if K_sol_cost > max_sol_cost:
                     max_sol_cost = K_sol_cost