0% found this document useful (0 votes)
12 views5 pages

Vlsi Assignment 1

Tellz about vlsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Vlsi Assignment 1

Tellz about vlsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

VLSI ASSIGNMENT 1

RIASHREE P
21D078

SYSTEM PARTITIONING – KERNIGHAN LIN ALGORITHM

KERNIGHAN LIN ALGORITHM


The Kernighan-Lin (KL) algorithm is a heuristic method for
partitioning a graph into two subsets to minimize the cut size (i.e., the
number of edges between the two subsets)
STEPS
1. Initial Partitioning:
o Divide the vertices into two subsets, A and B, each containing
half of the vertices.
2. Calculate D-values:
o For each vertex, compute the "D-value" which represents the
difference between the number of edges connecting the
vertex to the vertices in its own subset and the number of
edges connecting it to vertices in the opposite subset.
3. Pair Selection:
o Identify pairs of vertices, one from subset A and one from
subset B, that can be swapped to potentially improve the
partition.
o For each pair, calculate the gain (or loss) in the cut size if the
vertices are swapped.
4. Find Optimal Swaps:
o Determine the pair of vertices whose swap results in the
maximum gain in terms of reduced cut size.
o Perform the swap if it results in a positive gain.
5. Iterate:
o Repeat the pair selection and swap steps for a predefined
number of iterations or until no positive gain can be achieved.
6. Update Partitions:
o After completing the iterations, finalize the partition with the
minimum cut size.

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.

You might also like