--- /dev/null
+#!/usr/bin/python
+# -*- encoding: utf-8 -*-
+
+# Codé par 20-100 et Gaétan le 13/08/12
+# parce que parfois, à la Mèd, on fait des conneries
+
+import psycopg2
+import psycopg2.extras
+
+from dijkstra import dijkstra
+
+inf = float('Inf')
+
+def getcursor():
+ con = psycopg2.connect(database = "chopes")
+ cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
+ return con, cur
+
+
+def matrice(n):
+ con, cur = getcursor()
+ cur.execute("SELECT * FROM chopes;")
+ l = cur.fetchall()
+ m = [ [0 if i==j else inf for i in range(n)] for j in range(n)]
+ for (a,b) in l:
+ m[a][b] = 1 # Deux personnes qui se sont chopées sont à distance 1
+ m[b][a] = m[a][b] # Pour nous, les matrices seront symétriques. (Victime inconsciente : -10 pts)
+ return m
+
+
+def nom(id):
+ con, cur= getcursor()
+ cur.execute("SELECT nom FROM gens WHERE id = %s;", (id,))
+ return cur.fetchone()["nom"]
+
+def distances(m):
+ n = len(m)
+ dists = []
+ for i in range(n):
+ dists.append(dijkstra(m, i))
+ return dists
+
+def moyenne(dists, i):
+ nn = len([a for a in dists[i] if a!=inf])
+ return sum([dist for dist in dists[i] if dist!=inf])/float(nn-1) # On ne se compte pas soi-même
+
+if __name__ == "__main__":
+ con, cur = getcursor()
+ cur.execute("SELECT max(id) FROM gens;")
+ n = cur.fetchone()[0] + 1
+ mat = matrice(n)
+ dists = distances(mat)
--- /dev/null
+#!/usr/bin/python
+# -*- encoding: utf-8 -*-
+
+import heapq as hq
+inf = float('Inf')
+
+def dijkstra(G, s):
+ n = len(G)
+
+ Q = [(0, s)]
+
+ d = [inf for i in range(n)]
+ d[s]=0
+
+
+ while len(Q)!=0:
+ (cost, u) = hq.heappop(Q)
+
+ for v in range(n):
+ if d[v] > d[u] + G[u][v]:
+ d[v] = d[u] + G[u][v]
+ hq.heappush(Q, (d[v], v))
+
+ return d