Beispiel #1
0
from matplotlib import pyplot as plt
from matplotlib import animation, rcParams
import networkx as nx
from kruskal import *

rcParams['toolbar'] = 'None'
G=nx.read_edgelist("graph.edgelist")

# Make the MST, the History and the cycles(For animation steps)
history, mst, cycles = kruskal(G)

# Layout of the Graph
pos = nx.spring_layout(G)
# Lists for keeping track of the nodes and edges
visited_edges_cycles = []
visited_edges = []
# Function for making figure
def makeFigure():
    # Figure
    fig = plt.figure()
    fig.canvas.set_window_title("Kruskal Algorithm Animation")
    # Add ax1
    ax1 = fig.add_subplot(2,2,1)
    # Add ax1
    ax2 = fig.add_subplot(2,2,2)
    plt.sca(ax1)
    # Remove the ticks and tick labels
    ax1.set_xticks([])
    ax1.set_yticks([])
    ax2.set_xticks([])
    ax2.set_yticks([])
Beispiel #2
0
from matplotlib import pyplot as plt
from matplotlib import animation, rcParams
import networkx as nx
from prim import *
from kruskal import *

rcParams['toolbar'] = 'None'
# Create the graph from file
G = nx.read_edgelist("graph.edgelist")

# Make the prim_mst and the prim_history(For prim's animation steps)
prim_mst, prim_history = prim(G, 'A')
# Make the kruskal_mst and the kruskal_history(For krukal's animation steps)
kruskal_history, kruskal_mst, kruskal_cycles = kruskal(G)

# Layout of the Graph
pos = nx.spring_layout(G)
# Lists for keeping track of the nodes and edges
visited_edges_prim = []
visited_nodes_prim = []
edges_cycles_kruskal = []
visited_edges_kruskal = []


# Function for making figure
def makeFigure():
    # Figure
    fig = plt.figure()
    fig.canvas.set_window_title("MST algorithm comparison")
    # Add ax1 (Prim)
    ax1 = fig.add_subplot(2, 2, 1)
Beispiel #3
0
from kruskal import *
from auto_move import *
from start_position import *

# start pygame
pygame.init()
win = pygame.display.set_mode((width, height))
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('Maze')
win.fill(white)
pygame.display.update()

# Рисуем лабиринт
new_maze()
kruskal()
# Выход
a[n + 2][m + 1] = a[n + 3][m + 1] = 0

# Максимальный путь
max_i, max_j = max_cell(bfs())
a[max_i][max_j] = 5
update()  # <<<---

# Рисуем кнопки + Получаем список всех кнопок
bs = draw_buttons()

# Рисуем игроков
for i in range(players):
    draw_player(0, 0, i)
	def test_general_case(self):
		for arg in kruskal_test_data.values():
			N, E, edges, mstcost = read_input(arg)
			mincost, mst = kruskal(N, E, edges)
			self.assertEqual(mincost, mstcost)
	def test_self_edge(self):
		self.assertEqual(kruskal(1, 1, [edge(1, 0, 0)]), (0, []))
	def test_single_element(self):
		self.assertEqual(kruskal(2, 1, [edge(1, 0, 1)]),(1, [edge(1,0,1)]))
	def test_no_element(self):
		self.assertEqual(kruskal(0, 0, []), (0, []))
from kruskal import *

with open('aeropuertos1.in') as f:
    line = f.readline().strip()
    N, M, A = [int(x) for x in line.split(' ')]
    G = [[] for _ in range(N)]
    for _ in range(M):
        line = f.readline().strip()
        u, v, c = [int(x) for x in line.split(' ')]
        u -= 1
        v -= 1
        G[u].append((v, c))
        #G[v].append((u, c))
    print(G)

r, t = kruskal(G)

print(r, t)
s = 0
for _, _, C in t:
    s += C
for x in r:
    if x < 0:
        s += A

print(s)


Beispiel #9
0
 def test_self_edge(self):
     self.assertEqual(kruskal(1, 1, [edge(1, 0, 0)]), (0, []))
Beispiel #10
0
 def test_general_case(self):
     for arg in kruskal_test_data.values():
         N, E, edges, mstcost = read_input(arg)
         mincost, mst = kruskal(N, E, edges)
         self.assertEqual(mincost, mstcost)
Beispiel #11
0
 def test_no_element(self):
     self.assertEqual(kruskal(0, 0, []), (0, []))
Beispiel #12
0
 def test_single_element(self):
     self.assertEqual(kruskal(2, 1, [edge(1, 0, 1)]), (1, [edge(1, 0, 1)]))