Vlsi Assignment 1
Vlsi Assignment 1
RIASHREE P
21D078
CODE
import pandas as pd
import numpy as np
import random
import networkx as nx
from tqdm import tqdm
data = pd.read_csv("input_5.txt", sep=' ')
n = max(list(data.max()))
A = np.zeros((n + 1, n + 1))
for i in range(data.shape[0]):
x = data.iloc[i, 0]
y = data.iloc[i, 1]
A[x, y] = 1
A[y, x] = 1
v = list(range(n))
random.shuffle(v)
cut = int(n / 2)
n1, n2 = np.sort(v[:cut]), np.sort(v[cut:])
pairs = len(n1) * len(n2)
def cut_size(n1, n2):
r_old = 0
for i in n1:
for j in n2:
if A[i, j] == 1:
r_old += 1
return r_old
def best_swap(n1, n2):
mark = []
partition = []
cut_s = []
while len(mark) != pairs - 1:
r_best, ij, part = [], [], []
r_old = cut_size(n1, n2)
for i in range(len(n1)):
for j in range(len(n2)):
if ([n1[i], n2[j]] not in mark and [n2[j], n1[i]] not in mark):
n11, n22 = n1.copy(), n2.copy()
n11[i] = n2[j]
n22[j] = n1[i]
del_R = r_old - cut_size(n11, n22) # Change in cut size
r_best.append(del_R)
part.append([n11, n22])
ij.append([n11[i], n22[j]])
a = np.argmax(r_best)
c_part = part[a]
partition.append(c_part)
mark.append(ij[a])
cut_s.append(cut_size(c_part[0], c_part[1]))
n1, n2 = c_part[0], c_part[1]
return cut_s[np.argmin(cut_s)], partition[np.argmin(cut_s)]
def kernighan_lin(n1, n2):
cs = []
for _ in range(1): # Number of iterations
CutSize, partition = best_swap(n1, n2)
n1, n2 = partition[0], partition[1]
cs.append(CutSize)
# Check if cut size is converging
if len(cs) >= 10:
ts = cs[-5:]
if ts[0] == ts[1] == ts[2] == ts[3] == ts[4]:
break
return CutSize, partition
c, p = kernighan_lin(n1, n2)
p1, p2 = np.sort(p[0]), np.sort(p[1])
print(f"\nCut size is : {c}")
print(f"Partition 1 : {p1}")
print(f"Partition 2 : {p2}")
grp1 = p[0]
grp2 = p[1]
G = nx.Graph()
for i in range(len(data)):
G.add_edge(data.iloc[i, 0], data.iloc[i, 1])
for n in G.nodes():
G.nodes[n]['color'] = 'b' if n in grp1 else 'g'
colors = [node[1]['color'] for node in G.nodes(data=True)]
nx.draw(G, with_labels=True, node_color=colors)
INTPUT
Edges connecting vertex:
12
13
14
24
25
34
36
47
56
57
67
78
OUTPUT
Cut size = 8
Partition 1 = 1 2 3 4
Partition 1 = 5 6 7 8
CONCLUSION
The Kernighan-Lin algorithm is an effective heuristic for graph
partitioning that seeks to minimize the cut size by iteratively swapping
pairs of vertices between partitions. Although it may not always find the
optimal partition, it provides a practical solution for large and complex
graphs with a reasonable computational effort.