patternpythonMinor
Karger's min-cut algorithm implemented in python
Viewed 0 times
implementedcutminalgorithmpythonkarger
Problem
I implemented Karger's algorithm with the function
```
from numpy import inf
from random import choice
from copy import deepcopy
# We use the karger method for finding the minimum cut in a graph.
# Each row of "original_Graph" needs to match a vertice and each element of
# the row corrispond to the vertices to which it's connected.
# In addition in the first row there needs to be the first vertice, the second
# row the second vertice and so on.
# You also need to input the number of times you want the random "find_cut" to
# be performed.
def find_min_cut(original_Graph, num_repeats):
min_cut = inf
for ith_repeat in range(1, num_repeats + 1):
print ith_repeat, "th repeat"
Graph = deepcopy(original_Graph)
cut = find_cut(Graph)
if cut < min_cut:
min_cut = cut
return min_cut
def find_cut(Graph):
# list_vertices keeps the the list of vertices and can be used for
# selecting a vertice in the graph by using list_vertices.index(vertice).
# It basically helps to keep track of the vertices in the graph.
list_vertices = range(1, len(Graph) + 1)
# I contract the edges until there are only two vertices
for num_contraction in range(len(Graph) - 2):
contract_edge(Graph, list_vertices)
return count_cut(Graph)
def contract_edge(Graph, list_vertices):
# I choose randomly two vertices connected to select and edge.
rand_vertice = choice(list_vertices)
edge = [rand_vertice, choose_edge(Graph, rand_vertice, list_vertices)]
unite_edge(Graph, edge, list_vertices)
clean_graph(Graph, edge, list_vertices)
def count_cut(Graph):
# When you finished contracting the number of connection between the two
# vertices is the length of the row containing the connection of the
# vertices
return len(Graph[0])
def choose_edge(Graph, vertice, list_vertices):
# I choose a random connected
find_min_cut and it works, but I don't feel satisfied with the code I wrote. ```
from numpy import inf
from random import choice
from copy import deepcopy
# We use the karger method for finding the minimum cut in a graph.
# Each row of "original_Graph" needs to match a vertice and each element of
# the row corrispond to the vertices to which it's connected.
# In addition in the first row there needs to be the first vertice, the second
# row the second vertice and so on.
# You also need to input the number of times you want the random "find_cut" to
# be performed.
def find_min_cut(original_Graph, num_repeats):
min_cut = inf
for ith_repeat in range(1, num_repeats + 1):
print ith_repeat, "th repeat"
Graph = deepcopy(original_Graph)
cut = find_cut(Graph)
if cut < min_cut:
min_cut = cut
return min_cut
def find_cut(Graph):
# list_vertices keeps the the list of vertices and can be used for
# selecting a vertice in the graph by using list_vertices.index(vertice).
# It basically helps to keep track of the vertices in the graph.
list_vertices = range(1, len(Graph) + 1)
# I contract the edges until there are only two vertices
for num_contraction in range(len(Graph) - 2):
contract_edge(Graph, list_vertices)
return count_cut(Graph)
def contract_edge(Graph, list_vertices):
# I choose randomly two vertices connected to select and edge.
rand_vertice = choice(list_vertices)
edge = [rand_vertice, choose_edge(Graph, rand_vertice, list_vertices)]
unite_edge(Graph, edge, list_vertices)
clean_graph(Graph, edge, list_vertices)
def count_cut(Graph):
# When you finished contracting the number of connection between the two
# vertices is the length of the row containing the connection of the
# vertices
return len(Graph[0])
def choose_edge(Graph, vertice, list_vertices):
# I choose a random connected
Solution
contract_edge does not select edges with uniform distribution, because first a random vertex is selected and then a random edge of that vertex is chosen. This produces a skewed distribution on edges, with edges on low degree vertices having a higher chance of beeing selected than those of high degree vertices.Context
StackExchange Code Review Q#133760, answer score: 3
Revisions (0)
No revisions yet.