Skip to content

Commit

Permalink
✨ Trial
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanazafli committed May 25, 2022
1 parent c823766 commit 7d40817
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Project-02/kruskal.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def kruskalAlgo(self):
self.printSolution()

def printSolution(self):

print('DONE')
pass

class DisjointSet:
def __init__(self, vertices):
Expand Down Expand Up @@ -97,10 +96,11 @@ def union(self, x, y):
G.addEdge("H", "L", 2)
G.kruskalAlgo()

a=0
for s,d,w in G.MST:
a += int(w)
print(f'{s}-{d}: {w}')


print(f'Minimum yayılan ağacın toplam ağırlığı: {a}')

D = nx.Graph()

Expand Down
127 changes: 127 additions & 0 deletions Project-02/prims.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import matplotlib.pyplot as plt
import networkx as nx

class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
self.nodes = []
self.MST = []

def addEdge(self, s, d, w):
self.graph.append([s, d, w])

def addNode(self, value):
self.nodes.append(value)

def kruskalAlgo(self):
pass
# Buraya Prim Algoritmasnı yerleştirsen yeterli
self.printSolution()

def printSolution(self):
pass

class DisjointSet:
def __init__(self, vertices):
self.vertices = vertices
self.parent = {}
for v in vertices:
self.parent[v] = v
self.rank = dict.fromkeys(vertices, 0)

def find(self, item):
if self.parent[item] == item:
return item
else:
return self.find(self.parent[item])

def union(self, x, y):
xroot = self.find(x)
yroot = self.find(y)
if self.rank[xroot] < self.rank[yroot]:
self.parent[xroot] = yroot
elif self.rank[xroot] > self.rank[yroot]:
self.parent[yroot] = xroot
else:
self.parent[yroot] = xroot
self.rank[xroot] += 1

G = Graph(12)
G.addNode("A")
G.addNode("B")
G.addNode("C")
G.addNode("D")
G.addNode("E")
G.addNode("F")
G.addNode("G")
G.addNode("H")
G.addNode("I")
G.addNode("J")
G.addNode("K")
G.addNode("L")
G.addEdge("A", "B", 3)
G.addEdge("B", "C", 1)
G.addEdge("C", "D", 1)
G.addEdge("A", "E", 2)
G.addEdge("A", "F", 4)
G.addEdge("B", "F", 2)
G.addEdge("C", "F", 4)
G.addEdge("C", "G", 1)
G.addEdge("D", "G", 1)
G.addEdge("D", "H", 5)
G.addEdge("E", "F", 5)
G.addEdge("F", "G", 3)
G.addEdge("G", "H", 5)
G.addEdge("E", "I", 1)
G.addEdge("F", "I", 6)
G.addEdge("F", "J", 3)
G.addEdge("I", "J", 2)
G.addEdge("G", "J", 3)
G.addEdge("G", "K", 3)
G.addEdge("J", "K", 3)
G.addEdge("G", "L", 2)
G.addEdge("G", "L", 2)
G.addEdge("K", "L", 4)
G.addEdge("H", "L", 2)
G.kruskalAlgo()

a=0
for s,d,w in G.MST:
a += int(w)
print(f'{s}-{d}: {w}')
print(f'Minimum yayılan ağacın toplam ağırlığı: {a}')

D = nx.Graph()

for i in G.nodes:
D.add_node(i)

for s,d,w in G.MST:
D.add_edge(s,d, weight = w)

elarge = [(u, v) for (u, v, d) in D.edges(data=True)]
esmall = [(u, v) for (u, v, d) in D.edges(data=True)]

pos = nx.spring_layout(D, seed=7) # positions for all nodes - seed for reproducibility

# nodes
nx.draw_networkx_nodes(D, pos, node_size=700)

# edges
nx.draw_networkx_edges(D, pos, edgelist=elarge, width=6)
nx.draw_networkx_edges(
D, pos, edgelist=esmall, width=6, alpha=0.5, edge_color="b", style="dashed"
)

# node labels
nx.draw_networkx_labels(D, pos, font_size=20, font_family="sans-serif")
# edge weight labels
edge_labels = nx.get_edge_attributes(D, "weight")
nx.draw_networkx_edge_labels(D, pos, edge_labels)

ax = plt.gca()
ax.margins(0.08)
plt.axis("off")
plt.tight_layout()
plt.show()
80 changes: 80 additions & 0 deletions Project-02/trial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# A Python program for Prim's Minimum Spanning Tree (MST) algorithm.
# The program is for adjacency matrix representation of the graph

import sys # Library for INT_MAX

class Graph():

def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]

# A utility function to print the constructed MST stored in parent[]
def printMST(self, parent):
print ("Edge \tWeight")
for i in range(1, self.V):
print (parent[i], "-", i, "\t", self.graph[i][parent[i]])

# A utility function to find the vertex with
# minimum distance value, from the set of vertices
# not yet included in shortest path tree
def minKey(self, key, mstSet):

# Initialize min value
min = sys.maxsize

for v in range(self.V):
if key[v] < min and mstSet[v] == False:
min = key[v]
min_index = v

return min_index

# Function to construct and print MST for a graph
# represented using adjacency matrix representation
def primMST(self):

# Key values used to pick minimum weight edge in cut
key = [sys.maxsize] * self.V
parent = [None] * self.V # Array to store constructed MST
# Make key 0 so that this vertex is picked as first vertex
key[0] = 0
mstSet = [False] * self.V

parent[0] = -1 # First node is always the root of

for cout in range(self.V):

# Pick the minimum distance vertex from
# the set of vertices not yet processed.
# u is always equal to src in first iteration
u = self.minKey(key, mstSet)

# Put the minimum distance vertex in
# the shortest path tree
mstSet[u] = True

# Update dist value of the adjacent vertices
# of the picked vertex only if the current
# distance is greater than new distance and
# the vertex in not in the shortest path tree
for v in range(self.V):

# graph[u][v] is non zero only for adjacent vertices of m
# mstSet[v] is false for vertices not yet included in MST
# Update the key only if graph[u][v] is smaller than key[v]
if self.graph[u][v] > 0 and mstSet[v] == False and key[v] > self.graph[u][v]:
key[v] = self.graph[u][v]
parent[v] = u

self.printMST(parent)

g = Graph(5)
g.graph = [ [0, 2, 0, 6, 0],
[2, 0, 3, 8, 5],
[0, 3, 0, 0, 7],
[6, 8, 0, 0, 9],
[0, 5, 7, 9, 0]]

g.primMST();

0 comments on commit 7d40817

Please sign in to comment.