import liste_adiacenta as la from collections import deque n, L = la.citire(False, "graf4.in") col = [-1 for i in range(n)] tata = [-1 for i in range(n)] la.afisare(L) def BFS(start): global col start -= 1 q = deque() q.append(start) col[start] = 0 while len(q) > 0: k = q.popleft() #print(k + 1, end = " ") for x in L[k]: if col[x] == -1: col[x] = 1 - col[k] tata[x] = k q.append(x) elif col[x] == col[k]: #tata[k] = x return k return -1 if BFS(1) == -1: print("Bipartit")
import liste_adiacenta as la from collections import deque n, L = la.citire(True) viz = [0 for i in range(n)] tata = [-1 for i in range(n)] def BFS(start): global viz, tata, dist viz = [0 for i in range(n)] tata = [-1 for i in range(n)] start -= 1 q = deque() q.append(start) viz[start] = 1 print(start + 1, end=" ") while len(q) > 0: k = q.popleft() for x in L[k]: if viz[x] == 0: print(x + 1, end=" ") viz[x] = 1 tata[x] = k q.append(x) f = open("graf.in", "r") m = int(f.readline().split()[1]) # obtin numarul de muchii linii = f.read().split( "\n") # obtin lista liniilor din fisier, inafara de prima linie
import liste_adiacenta as la import heapq as heap n, L = la.citire() t = [-1 for i in range(n)] #vector tati d = [9999 for i in range(n)] viz = [0 for i in range(n)] start = 0 d[start] = 0 q = [] heap.heappush(q, (d[start], start)) while len(q): x = heap.heappop(q)[1] viz[x] = 1 for y in L[x]: i = y[0] j = y[1] if viz[i] == 0 and d[i] > j: t[i] = x d[i] = j heap.heappush(q, (d[i], i)) print("Cost: " + str(sum(d))) for i in range(n): if t[i] != -1: print(t[i] + 1, i + 1)
import liste_adiacenta as la n, L = la.citire(True, "graf3.in") d = [9999 for i in range(n)] t = [-1 for i in range(n)] start = 0 d[start] = 0 #la.afisare(L) for i in range(n - 1): for u in range(n): for x in L[u]: v = x[0] c = x[1] if d[u] + c < d[v]: d[v] = d[u] + c t[v] = u ok = -1 for u in range(n): if ok != -1: break for x in L[u]: v = x[0] c = x[1] if d[u] + c < d[v]: ok = v break
import liste_adiacenta as la from collections import deque n, L = la.citire(False) viz = [[0 for i in range(n)] for j in range(2)] tata = [-1 for i in range(n)] dist = [[99999 for i in range(n)] for j in range(2)] print(viz) print(dist) def BFS(start, l): global viz, tata, dist #viz = [[0 for i in range(n)] for j in range(2)] tata = [-1 for i in range(n)] #dist = [[99999 for i in range(n)] for j in range(2)] start -= 1 q = deque() q.append(start) viz[l][start] = 1 dist[l][start] = 0 print(start + 1, end=" ") while len(q) > 0: k = q.popleft() for x in L[k]: if viz[l][x] == 0: print(x + 1, end=" ") viz[l][x] = 1 dist[l][x] = dist[l][k] + 1 tata[x] = k
import liste_adiacenta as la n, L, ordine = la.citire() rez = [-1 for i in range(n)] def colorare(): rez[ordine[0]] = 0 liber = [False for i in range(n)] for u in ordine[1:]: for i in L[u]: if rez[i] != -1: liber[rez[i]] = True color = 0 while liber[color] == True: color += 1 rez[u] = color for i in L[u]: if rez[i] != -1: liber[rez[i]] = False return rez col = colorare() for i in range(n): print("Varful " + str(i + 1) + " are culoarea " + str(col[i]))