Last active
April 6, 2020 17:37
-
-
Save Sasszem/3f64de4ec15dbf4dc7441a073fbff869 to your computer and use it in GitHub Desktop.
Python monte-carlo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# gyors kombinatorikai modell a 10 élre | |
all_edges = [(i, j) for i in range(5) for j in range(5) if i!=j and i<j] | |
def negyszog_e(elek): | |
# akkor négyszög ha a négy él négy csúcsot ad meg és minden csúcs fokszáma 2 | |
pontok = {} | |
for i,j in elek: | |
pontok[i] = pontok.get(i, 0) + 1 | |
pontok[j] = pontok.get(j, 0) + 1 | |
return len(pontok)==4 and sum((1 if v!=2 else 0) for _, v in pontok.items())==0 | |
def metszi_e(el1, el2): | |
# egymást metszi két él ha az egyik végpontja és a kezdőpontja között van a másik pontosan egy végpontja | |
# mivel a,b-vel és b,a-val is lesz nézve | |
# így az egyik eldobható | |
if el1[0]>el2[0]: | |
return False | |
if el1[0]==el2[0] or el1[1]==el2[1]: | |
return False | |
if el2[0] < el1[1] and not el2[1]<el1[1]: | |
return True | |
return False | |
def konvex_e(elek): | |
# ha van két egymást metsző él, nem lehet konvex | |
return not any(metszi_e(a, b) and a!=b for a in elek for b in elek) | |
def eset(): | |
elek = random.sample(all_edges, 4) | |
return negyszog_e(elek) and konvex_e(elek) | |
N = 1000000 | |
db = 0 | |
for _ in range(N): | |
if eset(): | |
db += 1 | |
print(db/N) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment