SHARE
TWEET

v1

a guest Nov 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3.  
  4. def vrvz(vr, n): # verific daca exista macar vreun fiu nevizitat
  5.     for i in range(n):
  6.         if vr[i]==0:
  7.             return 1
  8.     return 0
  9.  
  10.  
  11. n = random.randrange(8,13)
  12.  
  13. print("numarul de noduri este: ", n)
  14. print("\n\n")
  15.  
  16. v = [0] * n  # vector de vizite
  17. v2 = [0] * n  # vector de tati
  18.  
  19. radin = random.randint(0,n)
  20. k1 = n
  21. v2[radin] = 100
  22.  
  23. k1 = k1 - 1
  24. # in acest while se umple random vectorul de tati
  25. test = 0
  26. while k1:
  27.     nivel = random.randint(0, k1-1)
  28.     for i in range(n):
  29.         if v2[i]:
  30.             r = i
  31.     i = random.randint(1, n - 1)
  32.     while nivel != 0:
  33.         while v2[i] != 0 and k1 != 0:
  34.             i = random.randint(1, n - 1)
  35.         v2[i] = r
  36.         nivel = nivel - 1
  37.         k1 = k1 - 1
  38.     if k1 == 1:
  39.         test = test + 1
  40.     if test == 2:
  41.         k1 = 0
  42.  
  43.  
  44. for i in range(n):
  45.     k = random.randint(1, n)
  46.     while k == i:
  47.         k = random.randint(1, n)
  48.     if v2[i] == 0:
  49.         v2[i] = k
  50.  
  51. for i in range(n - 1):
  52.     if v2[i] != 100:
  53.         if (v2[i] == v2[i+1]) and (random.randrange(20) % 2) != 0:
  54.             v2[i+1] = i
  55.  
  56. v2[radin] = 0
  57.  
  58.  
  59. print("Vectorul random de tati este:\n")
  60. print(v2)
  61.  
  62. r = radin
  63. t = 0
  64. df = []
  65. print("\n")
  66. # in acest while fac DF continuu
  67. print("Depth first continuu arata asa:\n")
  68. while (vrvz(v, n)) and (t < 30):
  69.     ok = 0
  70.     t = t + 1
  71.     df.append(r)
  72.     v[r] = 1
  73.     for i in range(n):
  74.         if (v[i] == 0) and (v2[i] == r):
  75.             r = i
  76.             ok = 1
  77.             i = n + 1
  78.     if not ok:
  79.         r = v2[r]
  80.  
  81. if r > n - 1:
  82.     r = r - 1
  83. while (v2[r] != radin) and (t < 30) and (v2[r] != 0):
  84.     df.append(r)
  85.     r = v2[r]
  86.     t = t + 1
  87.  
  88. df.append(r)
  89. df.append(radin)
  90. print(df)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top