Skip to content

Instantly share code, notes, and snippets.

@saburbutt
Created December 10, 2019 20:07
Show Gist options
  • Save saburbutt/e90f48ce168e9e155039b5416e533bcf to your computer and use it in GitHub Desktop.
Save saburbutt/e90f48ce168e9e155039b5416e533bcf to your computer and use it in GitHub Desktop.
Median of two arrays in logn with Tkinter
"""1. You are interested in analyzing some hard-to-obtain data from two sepa- rate databases.
Each database contains n numerical values—so there are 2n values total—and you may assume that no two values are the same.
You’d like to determine the median of this set of 2n values, which we will define here to be the n th smallest value.
However, the only way you can access these values is through queries to the databases.
In a single query, you can specify a value k to one of the two databases, and the chosen database will return the k th smallest value that it contains.
Since queries are expensive, you would like to compute the median using as few queries as possible.
Give an algorithm that finds the median value using at most O(log n) queries."""
import statistics
#First make two databases that contain numeric numbers with name database1 and database2
from tkinter import *
import time
root = Tk()
root.title("Median interface for logn by SaburB")
root.resizable(False, False)
root.wm_minsize(width=900,height=500)
"""############################################################################"""
"""Second section for K hop neigborhood of node given a vertex"""
w1 = LabelFrame(root, text= "")
w1.pack(fill="both", expand="yes")
def onclick(args):
if args == 1:
if (D1length < D2length):
print("Median will be: ", MedianS(D1sorted, D1length, D2sorted, D2length))
st = MedianS(D1sorted, D1length, D2sorted, D2length)
else:
print("Median will be : ", MedianS(D2sorted, D2length, D1sorted, D1length))
st = MedianS(D2sorted, D2length, D1sorted, D1length)
def MedianS(D1sorted, D1length, D2sorted, D2length):
min_index = 0
max_index = D1length
while (min_index <= max_index):
i = (min_index + max_index) // 2
j = ((D1length + D2length + 1) // 2) - i
#print("i will be ", min_index, "+", max_index , "//2 =", i)
#print("j will be ","(", D1length, "+", D2length, "+ 1) // 2 -" , i, "= ", j)
if (i < D1length and j > 0 and D2sorted[j - 1] > D1sorted[i]):
First_Array_i1 = Button(w1, text="A1", bg="orange")
First_Array_1_i1 = Button(w1, text="A2", bg="pink")
Second_Array_i1 = Button(w1, text="B1", bg="green")
Second_Array_1_i1 = Button(w1, text="B2", bg="pink")
First_Array_i1.grid(row=5, column=0, padx=0, pady=2, ipadx=i*20, ipady=5)
First_Array_1_i1.grid(row=5, column=1, padx=0, pady=2, ipadx=(D1length - i) * 20, ipady=5)
Second_Array_i1.grid(row=6, column=0, padx=0, pady=2, ipadx=j * 20, ipady=5)
Second_Array_1_i1.grid(row=6, column=1, padx=0, pady=2, ipadx= (D2length -j) *20, ipady=5)
s = "Since B1[j-1] > A1[i] " + "\n" + str(D2sorted[j-1]) + " > " + str(D1sorted[i]) + "\n" + "Hence min_index = i+1"
Result = Label(w1, text= s)
Result.grid(row=5, column=2, padx=0, pady=2, ipadx=210, ipady=5)
min_index = i + 1
elif (i > 0 and j < D2length and D2sorted[j] < D1sorted[i - 1]):
First_Array_i1 = Button(w1, text="A1", bg="orange")
First_Array_1_i1 = Button(w1, text="A2", bg="pink")
Second_Array_i1 = Button(w1, text="B1", bg="green")
Second_Array_1_i1 = Button(w1, text="B2", bg="pink")
First_Array_i1.grid(row=7, column=0, padx=0, pady=2, ipadx=i * 20, ipady=5)
First_Array_1_i1.grid(row=7, column=1, padx=0, pady=2, ipadx=(D1length - i) * 20, ipady=5)
Second_Array_i1.grid(row=8, column=0, padx=0, pady=2, ipadx=j * 20, ipady=5)
Second_Array_1_i1.grid(row=8, column=1, padx=0, pady=2, ipadx=(D2length - j) * 20, ipady=5)
s = "Since now B2[j] < A1[i-1] " + "\n" + str(D2sorted[j]) + "<" + str(D1sorted[i-1]) + "\n" +" Hence max_index = i-1"
Result = Label(w1, text=s)
Result.grid(row=7, column=2, padx=0, pady=2, ipadx=210, ipady=5)
max_index = i - 1
else:
if (i == 0):
return D2sorted[j - 1]
if (j == 0):
return D1sorted[i - 1]
else:
First_Array_i2 = Button(w1, text="A1", bg="orange")
First_Array_1_i2 = Button(w1, text="A2", bg="pink")
Second_Array_i2 = Button(w1, text="B1", bg="green")
Second_Array_1_i2 = Button(w1, text="B2", bg="pink")
First_Array_i2.grid(row=9, column=0, padx=0, pady=2, ipadx=i * 20, ipady=5)
First_Array_1_i2.grid(row=9, column=1, padx=0, pady=2, ipadx=(D1length - i) * 20, ipady=5)
Second_Array_i2.grid(row=10, column=0, padx=0, pady=2, ipadx=j * 20, ipady=5)
Second_Array_1_i2.grid(row=10, column=1, padx=0, pady=2, ipadx=(D2length - j) * 20, ipady=5)
s = "Now that we have the perfect division \n"+"The bigger value between D1sorted[i - 1] " + str(D1sorted[i - 1]) +" and D2sorted[j - 1] "+ str(D2sorted[j - 1]) + "\n" + " is the median"
Result = Label(w1, text=s)
Result.grid(row=9, column=2, padx=0, pady=2, ipadx=210, ipady=5)
return returnmax(D1sorted[i - 1], D2sorted[j - 1])
print("ERROR!!! ", "returning\n")
return 0
def returnmax(D1sorted, D2sorted):
#print(D1sorted, D2sorted)
return D1sorted if D1sorted > D2sorted else D2sorted
k1= open('database1.txt', 'r')
k2= open('database2.txt', 'r')
D1 = []
D2 = []
for n in k2.readlines():
D1.append(int(n))
for n in k1.readlines():
D2.append(int(n))
D1sorted = sorted(D1)
D2sorted = sorted(D2)
D1length = len(D1sorted)
D2length = len(D2sorted)
#median_label = Label(w1, text = "Median is " + str(st))
Explaination = Label(w1, text = "For two databases our median will be (n+m+1)/2 smallest element" +"\n"+ "Hence we divide our two database arrays in two partitions A1, B1 and A2, B2" )
Explaination2 = Label(w1, text = "We need to divide both the arrays such that" +"\n"+ "The last element of A1 is <= the first element of B2" +"\n"+ "Similarly, the last element of B1 should be <= than the first element of A2")
First_Array = Button(w1, text="A1", bg="orange")
First_Array_1 = Button(w1, text="A1", bg="orange")
Second_Array = Button(w1, text="B1", bg="green")
Second_Array_1 = Button(w1, text="B1", bg="green")
Next_iteration = Button(w1, text="Start", bg = "grey", command =lambda:onclick(1))
#Positioning of widgets in the Grid
Explaination.grid(row=1, column=2, padx=0, pady=2, ipadx=210, ipady=5)
Explaination2.grid(row=2, column=2, padx=0, pady=2, ipadx=210, ipady=5)
First_Array.grid(row=1, column=0, padx=0, pady=2, ipadx=210, ipady=5)
First_Array_1.grid(row=1, column=1, padx=0, pady=2, ipadx=210, ipady=5)
Second_Array.grid(row=2, column=0, padx=0, pady=2, ipadx=210, ipady=5)
Second_Array_1.grid(row=2, column=1, padx=0, pady=2, ipadx=210, ipady=5)
#median_label.grid(row= 9, column= 0, padx = 2, pady = 2, ipadx = 100)
Next_iteration.grid(row= 16, column= 1, padx = 2, pady = 20, ipadx = 100)
"""############################################################################"""
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment