برنامه نویسی گراف - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

برنامه نویسی گراف

0 امتیاز
سلام کسی میتونه تو نوشتن برنامه ای با این جزئیات در c++ منو راهنمایی کنه؟(با دادن یال های دو گراف ،ماتریس مجاورت اجتماع آن را مشخص کند)ممنون میشم هر کی بلد راهنماییم کنه
سوال شده اردیبهشت 27, 1399  بوسیله ی nazanini (امتیاز 13)   2 4 5

1 پاسخ

0 امتیاز
کد پایتونش خدتون تبدیلش کنید
import itertools

def graph_union(graph1, graph2):
    """calculates the union of two graphs represented by their adjacency matrices
    the edges with highest weights are retained.
    :graph1: List of Lists representing the adjacency matrix of a graph
             graph1 is not mutated by the function
    :graph2: List of Lists representing the adjacency matrix of a graph
             graph2 is not mutated by the function
    :returns: a newly constructed List of Lists representing the union of graph1 and graph2
    """
    union = []
    for g1, g2 in itertools.zip_longest(graph1, graph2):
        line = []
        g1 = g1 if g1 is not None else (0,)
        g2 = g2 if g2 is not None else (0,)
        for e1, e2 in itertools.zip_longest(g1, g2):
            e1 = e1 if e1 is not None else 0
            e2 = e2 if e2 is not None else 0
            line.append(max(e1, e2))
        union.append(line)
    return union

graph1 = [[0, 1, 2, 1, 9], [1, 0, 0, 6, 0], [2, 0, 0, 15, 2], [1, 6, 15, 0, 7], [9, 0, 2, 7, 0]] 
graph2 = [[0, 19, 1, 0, 12, 0], [19, 0, 2, 0, 0, 0], [1, 2, 0, 0, 2, 0], [0, 0, 0, 0, 3, 5], [12, 0, 2, 3, 0, 2], [0, 0, 0, 5, 2, 0]]

print(graph_intersection(graph1, graph2))
print(graph_union(graph1, graph2))

 

پاسخ داده شده اردیبهشت 27, 1399 بوسیله ی Soon (امتیاز 53)   1 8 10
ممنون ازتون اما من،دانشجو ترم 2 هستم هنوز پایتون کار نکردم.
...