Created
November 9, 2018 12:51
-
-
Save Et7f3/0b1b093d36c06f8105a006d8a32a77e7 to your computer and use it in GitHub Desktop.
TP5 universite paris diderot informatique
This file contains 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
using System; | |
using System.Linq; | |
namespace Main { | |
public class TP5 { | |
static Random rand = new Random(); | |
static int randRange(int a, int b) | |
{ | |
return (rand.Next(b - a - 1) + a); | |
} | |
public static void Main(string[] args) { | |
int[][] g = {new int[]{0, 1, 0}, new int[]{1, 0, 0}, new int[]{0, 0, 0}}; | |
Console.WriteLine(deepToString(CreateGraph(5))); | |
Console.WriteLine(friends_nbr(g, 0)); | |
Console.WriteLine(toString(friends(g, 0))); | |
Console.WriteLine(toString(popular(g))); | |
int[][] gr = {new int[]{0, 1, 1}, new int[]{1, 0, 1}, new int[]{0, 1, 1}}; | |
Console.WriteLine(toString(common_friends(gr, 0, 1))); | |
int[][] gr2 = add_user(gr, new int[]{0, 1, 2, 3}); | |
Console.WriteLine(deepToString(gr2)); | |
Console.WriteLine(toString(popular_noms(g, new String[]{"a", "b", "c"}))); | |
Console.WriteLine("abc"); | |
} | |
static string deepToString(int[][] a) | |
{ | |
return "[" + String.Join(",", a.Select(p => toString(p)).ToArray()) + "]"; | |
} | |
static string toString(int[] a) | |
{ | |
return "[" + String.Join(",", a.Select(p => p.ToString()).ToArray()) + "]"; | |
} | |
static string toString(string[] a) | |
{ | |
return "[" + String.Join(",", a.Select(p => p).ToArray()) + "]"; | |
} | |
static int[][] CreateGraph(int N) | |
{ | |
int n = randRange(0, N); | |
int[][] Graph = new int[N][]; | |
for (int i = 0; i < N; i++) { | |
Graph[i] = new int[N]; | |
} | |
while (n != 0) | |
{ | |
int i = randRange(0, N), j = randRange(0, N); | |
if (0 == Graph[i][j] && i != j) | |
{ | |
Graph[i][j] = Graph[j][i] = 1; | |
n--; | |
} | |
} | |
return Graph; | |
} | |
static int friends_nbr(int[][] R, int a) | |
{ | |
int ret = 0; | |
foreach (int c in R[a]) | |
ret += c; | |
return ret; | |
} | |
static int[] friends(int[][] R, int a) | |
{ | |
int[] f = new int[friends_nbr(R, a)]; | |
int c = 0; | |
for (int i = 0; i < R[a].Length; i++) | |
if (1 == R[a][i]) | |
f[c++] = i; | |
return f; | |
} | |
static int[] popular(int[][] R) | |
{ | |
int[] ret_tmp = new int[R.Length]; | |
int max_friend = 0; | |
int l = 0; | |
for (int a = 0; a < R.Length; a++) | |
{ | |
int c = friends_nbr(R, a); | |
if (c > max_friend) | |
{ | |
l = 1; | |
ret_tmp[0] = a; | |
max_friend = c; | |
} | |
else if (c == max_friend) | |
ret_tmp[l++] = a; | |
} | |
int[] ret = new int[l]; | |
for (; --l >= 0;) | |
ret[l] = ret_tmp[l]; | |
return ret; | |
} | |
static int[] common_friends(int[][] R, int a, int b) | |
{ | |
int f_a = friends_nbr(R, a), f_b = friends_nbr(R, b); | |
int[] f = new int[(f_a > f_b ? f_a : f_b) - R[a][b]]; | |
int c = 0; | |
for (int i = 0; i < R[a].Length; i ++) | |
if (1 == R[a][i] && 1 == R[b][i]) | |
f[c++] = i; | |
return f; | |
} | |
static int[][] add_user(int[][] R, int[] t) | |
{ | |
int u = R.Length; | |
int[][] Ret = new int[u + 1][]; | |
for (int i = 0; i < u + 1; i++) { | |
Ret[i] = new int[u + 1]; | |
} | |
for (int i = 0; i < u; i++) | |
for (int j = 0; j < u; j++) | |
Ret[i][j] = R[i][j]; | |
foreach (int f in t) | |
if (0 <= f && f < u) | |
Ret[f][u] = Ret[u][f] = 1; | |
return Ret; | |
} | |
static string[] popular_noms(int[][] R, string[] noms) | |
{ | |
int[] ret_tmp = new int[R.Length]; | |
int max_friend = 0; | |
int l = 0; | |
for (int a = 0; a < R.Length; a++) | |
{ | |
int c = friends_nbr(R, a); | |
if (c > max_friend) | |
{ | |
l = 1; | |
ret_tmp[0] = a; | |
max_friend = c; | |
} | |
else if (c == max_friend) | |
ret_tmp[l++] = a; | |
} | |
string[] ret = new string[l]; | |
for (; --l >= 0;) | |
ret[l] = noms[ret_tmp[l]]; | |
return ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment