Пример #1
0
def getVertices():
    nVertices=ent_nVertices.get()
    global graph
    graph=kruskal.Graph(int(nVertices))
    frm_nVertices.pack_forget()
    lbl_graphMade = tk.Label(mainWindow,text="Graph initialised with " + str(nVertices) + " vertices")
    lbl_graphMade.pack()
    btn_addEdge.pack(side="top")
    btn_getEdgeValues.pack(side="left")
    btn_buildTree.pack(side="bottom")
Пример #2
0
    def traverse_using_kruskal(self):
        start_time = time.time()
        g2 = kruskal.Graph(self.n)
        for (u, v, w) in self.edges:
            g2.add_edge(u, v, w)

        edges = g2.mst()
        final_cost = 0
        steps_size = [0]
        path = []
        dfs_path = []
        new_graph = {i: [] for i in range(self.n + 1)}
        for (u, v, w) in edges:
            new_graph[u].append((v, w))
            new_graph[v].append((u, w))
        starting_v = np.random.randint(1, self.n + 1)
        self.dfs(starting_v, starting_v, new_graph, dfs_path, steps_size)
        for i in range(len(dfs_path) - 1):
            c = self.get_cost(dfs_path[i], dfs_path[i + 1])
            path.append(
                str(dfs_path[i]) + '-->' + str(dfs_path[i + 1]) + " koszt: " +
                str(c))
            final_cost += c

        print("KRUSKAL:\n", '\n'.join(path), '\n', file=sys.stderr, sep='')
        print("KRUSKAL")
        print(sum([e[2] for e in edges]))
        print("Kroki: ",
              steps_size[0],
              ' koszt: ',
              final_cost,
              ' Pamięć: ',
              sys.getsizeof(edges) + sys.getsizeof(new_graph) +
              sys.getsizeof(g2),
              'B',
              ' czas: ',
              time.time() - start_time,
              's',
              sep='')
        print()
Пример #3
0
def main():
    if len(sys.argv) != 2: 
        print("python main.py -p|k\n")
    else: 
        if sys.argv[1] not in ["-p","-k"]:
            print("python main.py -p|k")
        else: 
            n = int(input())
            m = int(input())
            if sys.argv[1] == "-p":
                g = prim.Graph(n)
            else:
                g = kruskal.Graph(n)
            prices = {}
            for i in range(m):
                (u,v,w) = input().split(' ')
                u = u.replace('\r','')
                v = v.replace('\r','')
                w = w.replace('\r','')
                u = int(u)
                #u+=1
                v = int(v)
                #v+=1
                w = float(w)
                prices[(u,v)] = w
                g.add_edge(u,v,w)
            
            start = int(input())
            edges = g.mst()
            edges.sort()
            size = 0
            for ((v, u, w)) in edges:
                if u != -1: 
                    print(min(u,v), max(u,v), w)
                    size += w
        
            print(size)
Пример #4
0
import tkinter as tk
import kruskal

mainWindow = tk.Tk()
mainWindow.title("Kruskal's Minimum Spanning Tree")

nVertices=0
nEdges=0

frames = []
edgeValues = []
labels = []

graph = kruskal.Graph(nVertices)

def getVertices():
    nVertices=ent_nVertices.get()
    global graph
    graph=kruskal.Graph(int(nVertices))
    frm_nVertices.pack_forget()
    lbl_graphMade = tk.Label(mainWindow,text="Graph initialised with " + str(nVertices) + " vertices")
    lbl_graphMade.pack()
    btn_addEdge.pack(side="top")
    btn_getEdgeValues.pack(side="left")
    btn_buildTree.pack(side="bottom")

def createEdgeValues():
    global edgeValues
    global frames
    global labels
Пример #5
0
afiseazameniu.afiseazameniu()
alegere = raw_input()
file_paths = {
    'a': 'date1.txt',
    'b': 'date2.txt',
    'c': 'date3.txt',
    'd': 'date4.txt',
    'e': 'date5.txt',
    'f': 'date6.txt',
    'g': 'date7.txt',
    'h': 'date8.txt',
    'i': 'date9.txt',
    'j': 'date10.txt',
}
file_path = file_paths.get(alegere)
if not file_path:
    print("Input invalid")
    exit(1)

with open(file_path, 'r') as fin:
    varfuri = int(fin.readline())
    g = kruskal.Graph(varfuri)
    muchii = int(fin.readline())
    i = 0
    while i < muchii:
        g.adaugamuchie(int(fin.readline()), int(fin.readline()),
                       int(fin.readline()))
        i = i + 1
start = time.time()
g.arboreacoperireminima()
print("Timpul cat a rulat programul: %f" % (time.time() - start))