You are on page 1of 8

array numpy

fMatAdj MatAdj G

fMatAdj

n-1

L = [[3,5],[1,5],[2,3],[2,1]]

L
fMatAdj fMatAdj(6,L)

fMatAdj L

[i,i]
[i,j] [j,i]

1 def fMatAdj(n, L):


2 # COMPLETE

MatAdj n
n L

MatAdj

1 # COMPLETE
[0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 1]
[0, 1, 0, 1, 0, 0]
[0, 0, 1, 0, 0, 1]
[0, 0, 0, 0, 0, 0]
[0, 1, 0, 1, 0, 0]

n G MatAdj

fDegSom
n MatAdj D n D[i]
i
1 def fDegSom(n, MatAdj):
2 # COMPLETE
3
4 # example of an adjacency matrix for a graph with 4 vertices
5 MatAdj = [[0, 1, 1, 0],
6 [1, 0, 0, 1],
7 [1, 0, 0, 1],
8 [0, 1, 1, 0]]
9
10 # call the fDegSom function to compute the degree of each vertex
11 n = 4
12 D = fDegSom(n, MatAdj)
13
14 # print the resulting list of vertex degrees
15 print(D)
16
[2, 2, 2, 2]
G

DD
[i,d(i)] i
d(i)

DD
DD

DD S
DD

DD

1 def chromatic_number(G):
2 degrees = { v: len(G[v]) for v in G}
3 vertices = sorted(G,key=lambda v:degrees[v],reverse=True)
4 colors = {}
5
6 print(degrees)
7 print(vertices)
8
9 for vertex in vertices:
10 # colors that used by neighbors to the vertes
11 used = {colors.get(neighbour) for neighbour in G[vertex]}
12 available_colors = {1,2,3,4,5} - used
13 color = min(available_colors)
14 colors[vertex] = color
14 colors[vertex] = color
15
16 print ('Colors are ', colors)
17 return max(colors.values())
18
19
20 G_left = { 1: {5,6}, 2: {4,6}, 3: {4,5}, 4: {1,5}, 5: {1,3}, 6: {1,2} }
21 print('Max number of colors for graph on the left is ', chromatic_number(G_left))
22 G_right = { 1: {3,4,5}, 2: {3}, 3: {1,4}, 4: {3,1,5}, 5: {1,4} }
23 print('Max number of colors for graph on the right is ', chromatic_number(G_right))
{1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 2}
[1, 2, 3, 4, 5, 6]
Colors are {1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 2}
Max number of colors for graph on the left is 2
{1: 3, 2: 1, 3: 2, 4: 3, 5: 2}
[1, 4, 3, 5, 2]
Colors are {1: 1, 4: 2, 3: 3, 5: 3, 2: 1}
Max number of colors for graph on the right is 3

1 G_left = # COMPLETE
2 print(chromatic_number(G_left))
2
2

1 G_right = # COMPLETE
2 print(chromatic_number(G_right))
3

1 G_4 = {
2 1: {2,3,4},
3 2: {1,3,4,5},
4 3: {1,2,4,5,6},
5 4: {1,2,3},
6 5: {2,3,7},
7 6: {3,4,7},
8 7: {5,6}
9 }
10 print(chromatic_number(G_4))
11
12 G_countries = { 'Portugal': {'Spain'}, 'Spain': {'Portugal','France'},
13 'France': {'Spain','Belgium','Luxembourg','Switzerland','Germany','Italy'},
14 'United Kingdom' : {'Ireland'}, 'Ireland':{'United Kingdom'},
15 'Belgium' : {'France','Netherlands','Germany'},
16 'Switzerland' : {'France','Germany','Italy'},
17 'Germany' :{'France','Belgium','Netherlands','Switzerland'},
18 'Netherlands': {'Belgium','Germany'},
19 'Italy' : {'France','Switzerland'}
20 }
21 print(chromatic_number(G_countries))
Colors are {3: 1, 2: 2, 1: 3, 4: 4, 5: 3, 6: 2, 7: 1}
4
Colors are {'France': 1, 'Germany': 2, 'Belgium': 3, 'Switzerland': 3, 'Spain': 2, 'Netherlands': 1, 'Ital
3
1 G_aquarium={
2 'A': {'A','E','F'},
3 'B': {'B','C','D','H'},
4 'C': {'B','C','E'},
5 'D': {'B','D','F','G'},
6 'E': {'A','C','E','H'},
7 'F': {'A','D','F','G','H'},
8 'G': {'D','F','G','H'},
9 'H': {'B','E','F','G','H'}
10 }
11 print(chromatic_number(G_aquarium))
Colors are {'F': 1, 'H': 2, 'B': 1, 'D': 2, 'E': 1, 'G': 3, 'A': 2, 'C': 2}
3

1 # COMPLETE
1 # COMPLETE

1 # COMPLETE

You might also like