Dynamic (interactive) graph illustration (Jupyter + ipywidgets interact)

In many cases, the illustrations are effective in solving the problem of competitive programming. At this time, it is often more effective to change the input value. The following interface can be realized immediately by using jupyter. gif3.gif

Subject

Educational Codeforces Round 80 A.Deadline https://codeforces.com/contest/1288/problem/A When N, d, x are positive integers greater than or equal to 1 and $ d \ leq x , find out if there is an x that satisfies the following. $ n \ geq \ left \ lceil \ frac {d} {x + 1} \ right \ rceil + x $$ where $ \ lceil a \ rceil $ is the smallest integer greater than or equal to a (not a Gaussian function). Yes (eg 3.1 for 4, 3 for 3)

Static illustration

Examples of NO and YES in the sample problem are shown on the left and right, respectively. This can be done immediately by simply using the matplotlib function. image.png Blue is the line of $ y = n $, yellow is $ y = \ left \ lceil \ frac {d} {x + 1} \ right \ rceil + x $, and green is the plot before the ceiling function is applied.

By showing in this way, it can be seen that YES / NO is determined by the existence of the intersection of the blue line and the yellow line. (Of course, this is self-evident because it's an inequality ...)

%matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import interact
import numpy as np

fig, (axL, axR) = plt.subplots(ncols=2, figsize=(10,4))

n,d = 5,11
xaxis = yaxis = 8
x = np.linspace(1, n, num = 100)
y1 = [n] *100
y2 = np.ceil(d / (x + 1) ) + x
y3 = d / (x + 1) + x
axL.plot(x, y1)
axL.plot(x, y2)
axL.plot(x, y3)
axL.set_xlim(0, xaxis)
axL.set_ylim(0, yaxis)
axL.grid(True)

n2,d2 = 5,4
xaxis = yaxis = 8
x2 = np.linspace(1, n2, num = 100)
y21 = [n2] *100
y22 = np.ceil(d2 / (x2 + 1) ) + x2
y23 = d2 / (x2 + 1) + x2
axR.plot(x2, y21)
axR.plot(x2, y22)
axR.plot(x2, y23)
axR.set_xlim(0, xaxis)
axR.set_ylim(0, yaxis)
axR.grid(True)

fig.show()

Dynamic illustration

You can use interact to create a slider that moves n and d variably.

gif2.gif

%matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import interact
import numpy as np

d = 20
n = 20
import math

xaxis = yaxis = 20
nlimit = 20
dlimit = 100

@interact(n=(1, nlimit), d = (1,dlimit))
def f(n, d):
    x = np.linspace(1, n, num = 100)
    y1 = [n] * 100
    y2 = np.ceil(d / (x + 1) ) + (x)
    y3 = d / (x + 1) + x
    plt.xlabel("x-axis")
    plt.ylabel(" y-axis")
    plt.plot(x, y1)
    plt.plot(x, y2)
    plt.plot(x, y3)
    plt.xlim(0, xaxis)
    plt.ylim(0, yaxis)
    plt.grid(True)
    plt.figure(figsize=(1,1))
    plt.show()

Recommended Posts

Dynamic (interactive) graph illustration (Jupyter + ipywidgets interact)
[PyTorch] Sample ⑨ ~ Dynamic graph ~