Last active
November 9, 2018 12:53
-
-
Save Et7f3/ab0f9c65aaee2e0ccba1553294234305 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
import java.util.Random; | |
import java.util.Arrays; | |
public class TP5 | |
{ | |
static Random rand = new Random(); | |
static int randRange(int a, int b) | |
{ | |
return (rand.nextInt(b - a) + a); | |
} | |
public static void main(String[] args) | |
{ | |
int[][] g = {{0, 1, 0}, {1, 0, 0}, {0, 0, 0}}; | |
System.out.println(Arrays.deepToString(CreateGraph(5))); | |
System.out.println(friends_nbr(g, 0)); | |
System.out.println(Arrays.toString(friends(g, 0))); | |
System.out.println(Arrays.toString(popular(g))); | |
int[][] gr = {{0, 1, 1}, {1, 0, 1}, {0, 1, 1}}; | |
System.out.println(Arrays.toString(common_friends(gr, 0, 1))); | |
int[][] gr2 = add_user(gr, new int[]{0, 1, 2, 3}); | |
System.out.println(Arrays.deepToString(gr2)); | |
System.out.println(Arrays.toString(popular_noms(g, new String[]{"a", "b", "c"}))); | |
System.out.println("abc"); | |
} | |
static int[][] CreateGraph(int N) | |
{ | |
int n = randRange(0, N); | |
int[][] Graph = new int[N][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; | |
for (int c : 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][u + 1]; | |
for (int i = 0; i < u; i++) | |
for (int j = 0; j < u; j++) | |
Ret[i][j] = R[i][j]; | |
for (int f : 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; | |
}/* | |
static int[] invite(int[][] R, int a) | |
{ | |
int[] ret_tmp = new int[R.length]; | |
int c = 0; | |
for (int i = 0; i < R.length; i++) | |
if (1 == R[a][i]) | |
{ | |
R[i][a] = 0; | |
for (int j = 0; j < R.length; j++) | |
if (1 == R[i][j]) | |
{ | |
ret_tmp[j] = j; | |
c++; | |
} | |
} | |
int[] ret = new int[c]; | |
for (int i = 0; i < ret_tmp.length; i++) | |
if (0 != ret_tmp[i]) | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment