Now suppose you\'re given an n£n grid graph G. (An n£n grid graph is just the ad
ID: 3856962 • Letter: N
Question
Now suppose you're given an n£n grid graph G. (An n£n grid graph is just the adjacency
graph of an n £ n chessboard. To be completely precise, it is a graph whose node set is the
set of all ordered pairs of natural number (i; j) where 1 · i · n and 1 · j · n; the nodes
(i; j) and (k; l) are joined by an edge if and only if ji ¡ kj + jj ¡ lj = 1:.)
We use some of the terminology of the previous question. Again, each node v is labeled
by a real number xv; you may assume that all these labels are distinct. Show how to ¯nd a
local minimum of G using only O(n) probes to the nodes of G. (Note that G has n2 nodes.)
2
Explanation / Answer
import matplotlib.pyplot as plt
import numpy as np
import pandas
from matplotlib.table import Table
def main():
data = pandas.DataFrame(np.random.random((12,8)),
columns=['A','B','C','D','E','F','G','H'])
checkerboard_table(data)
plt.show()
def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['yellow', 'white']):
fig, ax = plt.subplots()
ax.set_axis_off()
tb = Table(ax, bbox=[0,0,1,1])
nrows, ncols = data.shape
width, height = 1.0 / ncols, 1.0 / nrows
# Add cells
for (i,j), val in np.ndenumerate(data):
# Index either the first or second item of bkg_colors based on
# a checker board pattern
idx = [j % 2, (j + 1) % 2][i % 2]
color = bkg_colors[idx]
tb.add_cell(i, j, width, height, text=fmt.format(val),
loc='center', facecolor=color)
# Row Labels...
for i, label in enumerate(data.index):
tb.add_cell(i, -1, width, height, text=label, loc='right',
edgecolor='none', facecolor='none')
# Column Labels...
for j, label in enumerate(data.columns):
tb.add_cell(-1, j, width, height/2, text=label, loc='center',
edgecolor='none', facecolor='none')
ax.add_table(tb)
return fig
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.