Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 1091ef81 rédigé par Josik's avatar Josik
Parcourir les fichiers

dijkstra algorithm

parent 4a4bab35
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
function Dijkstra(roads, source, dest) {
for(let pers of window.jsonGlobal){
for(let idParent of pers.parents){
makeRoad(pers.id, idParent, 1);
}
for(let idChild of pers.children){
makeRoad(pers.id, idChild, 1);
}
for(let idPartner of pers.partners){
makeRoad(pers.id, idPartner, 1);
}
}
let inf = Number.POSITIVE_INFINITY;
let distance = {};
let done = {};
let pred = {};
for (let i in roads) {
// Unknown distance function from source to i.
distance[i] = inf;
pred[i] = 0;
done[i] = false;
}
// Distance from source to source = 0
distance[source] = 0;
for (i in roads) {
let minDist = inf, closest;
for (let j in roads) {
if (!done[j]) {
if (distance[j] <= minDist) {
minDist = distance[j];
closest = j;
}
}
}
done[closest] = true;
if (closest === dest) {
break;
}
let neighbors = roadsFrom(closest);
for (let nb in neighbors) {
let w = neighbors[nb];
if (!done[nb]) {
if (distance[closest] + w < distance[nb]) {
distance[nb] = distance[closest] + w;
pred[nb] = closest;
}
}
}
}
let traveled = [];
// Done, now print.
i = dest;
if (distance[i] < inf) {
const person = getPersById(Number(i));
traveled.push(person);
let thePath = person.name;
let place = i;
while (place !== source) {
place = pred[place];
if (place !== source) {
const pers = getPersById(Number(place));
traveled.push(pers);
thePath = pers.name + '->' + thePath;
}
}
const p = getPersById(Number(place));
thePath = p.name + '->' + thePath;
const pSource = getPersById(Number(source));
traveled.push(pSource)
console.log("Nombre de degrés de " + pSource.name + "-->" + getPersById(Number(dest)).name + " : " +
distance[i] + ' (' + thePath + ')');
} else {
console.log("Pas de la même famille");
}
return traveled.reverse();
}
let roads = {};
function makeRoad(from, to, length) {
function addRoad(from, to) {
if (!(from in roads)) {
roads[from] = {};
}
roads[from][to] = length;
}
addRoad(from, to);
addRoad(to, from);
}
function makeRoads(start) {
for (let i = 1; i < arguments.length; i += 2) {
makeRoad(start, arguments[i], arguments[i + 1]);
}
}
function roadsFrom(place) {
let found = roads[place];
if (found === undefined) {
print("No place named '" + place + "' found.");
} else {
return found;
}
}
\ No newline at end of file
......@@ -462,78 +462,90 @@ function createJsonGlobal(json){
family.push(dirigeant);
seen[dirigeant.id] = true;
for(let i in dirigeant.parents)
for(let i of dirigeant.parents)
{
let parent = getPersById(dirigeant.parents[i]);
if(!seen[parent.id]){
let parent = getPersById(i);
if(!seen[i]){
family.push(parent);
seen[parent.id] = true;
for(let j in parent.parents){ // grands-parents dirigeant
let grandparent = getPersById(parent.parents[j]);
if(!seen[grandparent.id]){
seen[i] = true;
for(let j of parent.parents){ // grands-parents dirigeant
let grandparent = getPersById(j);
if(!seen[j]){
family.push(grandparent);
seen[grandparent.id] = true;
for(let u in grandparent.children){ // oncles et tantes du dirigeant
let uncle = getPersById(grandparent.children[u]);
if(!seen[uncle.id]){
seen[j] = true;
for(let u of grandparent.children){ // oncles et tantes du dirigeant
let uncle = getPersById(u);
if(!seen[u]){
family.push(uncle);
seen[uncle.id] = true;
for(let j in uncle.children){ // cousins du dirigeant
let nephew = getPersById(uncle.children[j]);
if(!seen[nephew.id]){
family.push(nephew);
seen[nephew.id] = true;
}
seen[u] = true;
for(let jU of uncle.children){ // cousins du dirigeant
let nephew = getPersById(jU);
if(!seen[jU]){ family.push(nephew); seen[jU] = true;}
}
for(let j in uncle.partners){ // femmes et maris des oncles et tantes
let partner = getPersById(uncle.partners[j]);
if(!seen[partner.id]){
family.push(partner);
seen[partner.id] = true;
}
for(let jP of uncle.partners){ // femmes et maris des oncles et tantes
let partner = getPersById(jP);
if(!seen[jP]){ family.push(partner); seen[jP] = true;}
}
}
}
}
}
for(let j in parent.children){ // freres soeurs du dirigeant
let bro = getPersById(parent.children[j]);
if(!seen[bro.id]){
for(let broId of parent.children){ // freres soeurs du dirigeant
let bro = getPersById(broId);
if(!seen[broId]){
family.push(bro);
seen[bro.id] = true;
for(let j in bro.children){ // mari ou femme des freres et soeur du dirigeant
let child = getPersById(bro.children[j]);
if(!seen[child.id]){
family.push(child);
seen[child.id] = true;
}
seen[broId] = true;
for(let nephewId of bro.children){ // neveux et nièces du dirigeant
let nephew = getPersById(nephewId);
if(!seen[nephewId]){ family.push(nephew); seen[nephewId] = true;}
}
for(let j in bro.partners){ // mari ou femme des freres et soeur du dirigeant
let partner = getPersById(bro.partners[j]);
if(!seen[partner.id]){
family.push(partner);
seen[partner.id] = true;
}
for(let j of bro.partners){ // mari ou femme des freres et soeur du dirigeant
let partner = getPersById(j);
if(!seen[j]){ family.push(partner); seen[j] = true;}
}
}
}
}
}
for(let j in dirigeant.children){ //enfants dirigeant
let enfantDirigeant = getPersById(dirigeant.children[j]);
if(!seen[enfantDirigeant.id]){
for(let j of dirigeant.children){ //enfants dirigeant
let enfantDirigeant = getPersById(j);
if(!seen[j]){
family.push(enfantDirigeant);
seen[enfantDirigeant.id] = true;
seen[j] = true;
for(let gchId of enfantDirigeant.children){ //petits enfants dirigeant
let gch = getPersById(gchId);
if(!seen[gchId]){
family.push(gch);
seen[gchId] = true;
for(let spId of gch.partners){ // époux des petits enfants du dirigeant
let sp = getPersById(spId);
if(!seen[gchId]){
family.push(sp);
seen[spId] = true;
}
}
}
}
}
for(let chSpId of enfantDirigeant.partners){ // gendre et bru du dirigeant
let chSp = getPersById(chSpId);
if(!seen[chSpId]){
family.push(chSp);
seen[chSpId] = true;
}
}
}
for(let j in dirigeant.partners){ // mari ou femme du dirigeant
let partner = getPersById(dirigeant.partners[j]);
if(!seen[partner.id]){
for(let j of dirigeant.partners){ // mari ou femme du dirigeant
let partner = getPersById(j);
if(!seen[j]){
family.push(partner);
seen[partner.id] = true;
seen[j] = true;
}
}
// on enlève les liens vers des personnes extérieures au graphe choisi précédemment
let identifiants = [];
for(let p of family){
identifiants.push(p.id);
......@@ -557,6 +569,7 @@ function createJsonGlobal(json){
}
return family;
}
function getMultipleFamily(leaders){
if(leaders.length == 1){
return getFamily(leaders[0]);
......@@ -577,6 +590,8 @@ function createJsonGlobal(json){
bigFamily[i].children = Array.from(set);
set = new Set(bigFamily[i].parents.concat(bigFamily[j].parents));
bigFamily[i].parents = Array.from(set);
set = new Set(bigFamily[i].partners.concat(bigFamily[j].partners));
bigFamily[i].partners = Array.from(set);
bigFamily.splice(j,1);
}
}
......@@ -591,18 +606,17 @@ function createJsonGlobal(json){
if(person1.name === person2.name){
return true;
}
let trouve = false;
if(list.indexOf(person1.id) < 0){
list.push(person1.id)
if(person1.hasOwnProperty('parents')){
for(let idParent of person1.parents){
let parent = getPersById(idParent);
if(list.indexOf(parent.id) < 0 && (parent.hasOwnProperty('parents') || parent.hasOwnProperty('children'))){
if(isInFamily(parent, person2, list)){
relations.push("parent's")
degree++;
return true;
relations.push("parent's");
trouve = true;
return trouve;
}
}
}
......@@ -613,9 +627,9 @@ function createJsonGlobal(json){
let child = getPersById(idChildren);
if(list.indexOf(child.id) < 0 && (child.hasOwnProperty('children') || child.hasOwnProperty('parents'))){
if(isInFamily(child, person2, list)){
degree++;
relations.push("child's")
return true;
relations.push("child's");
trouve = true;
return trouve;
}
}
}
......@@ -625,10 +639,9 @@ function createJsonGlobal(json){
let partner = getPersById(idPartner);
if(list.indexOf(partner.id) < 0 && (partner.hasOwnProperty('parents') || partner.hasOwnProperty('children'))){
if(isInFamily(partner, person2, list)){
degree++;
relations.push("partner's");
return true;
trouve = true;
return trouve;
}
}
......@@ -636,11 +649,12 @@ function createJsonGlobal(json){
}
}
return false;
return trouve;
}
/**
* getter de personne par nom
*/
......@@ -661,6 +675,32 @@ function createJsonGlobal(json){
}
}
}
function listRelations(traveled){
for(let i = 0; i < traveled.length-1; i++){
const pers = traveled[i];
for(let idParent of pers.parents){
const parent = getPersById(idParent);
if(parent.name === traveled[i+1].name){
console.log("parent");
}
}
for(let idChild of pers.children){
const child = getPersById(idChild);
if(child.name === traveled[i+1].name){
console.log("child");
}
}
for(let idPartner of pers.partners){
const partner = getPersById(idPartner);
if(partner.name === traveled[i+1].name){
console.log("partner");
}
}
}
}
/**
* construction du graphe pour le dirigeant donné en paramètre
*/
......@@ -669,18 +709,29 @@ function createJsonGlobal(json){
tree = [];
degree = 0;
relations = [];
roads = {}
if(typeof(leaders) === "object"){
tree = getMultipleFamily(leaders);
console.log(isInFamily(getPersByName(leaders[0]),getPersByName(leaders[1]),new Array()));
relations = relations.reverse().join(" ");
relations = relations.replace("parent's parent's parent's","great-grandparent's");
/* relations = relations.replace("parent's parent's parent's","great-grandparent's");
relations = relations.replace("parent's parent's","grandparent's");
relations = relations.replace("child's child's child's","great-grandchild's");
relations = relations.replace("child's child's","grandchild's");
relations = relations.replace("parent's child's","sibling's");
relations = relations.replace("grandparent's grandchild's", "cousin's")
relations = relations.replace("cousin's parent's", "uncles or aunt 's")*/
const p1 = getPersByName(leaders[0]);
const p2 = getPersByName(leaders[1]);
const traveled = Dijkstra(roads,p1.id + "", p2.id + "");
listRelations(traveled)
}
else if(typeof(leaders) === "string"){
tree = getFamily(leaders);
}
build_graph();
rank();
normalize_layers();
......
function Dijkstra(roads, source, dest) {
for(let pers of window.jsonGlobal){
for(let idParent of pers.parents){
makeRoad(pers.id, idParent, 1);
}
for(let idChild of pers.children){
makeRoad(pers.id, idChild, 1);
}
for(let idPartner of pers.partners){
makeRoad(pers.id, idPartner, 1);
}
}
let inf = Number.POSITIVE_INFINITY;
let distance = {};
let done = {};
let pred = {};
for (let i in roads) {
// Unknown distance function from source to i.
distance[i] = inf;
pred[i] = 0;
done[i] = false;
}
// Distance from source to source = 0
distance[source] = 0;
for (i in roads) {
let minDist = inf, closest;
for (let j in roads) {
if (!done[j]) {
if (distance[j] <= minDist) {
minDist = distance[j];
closest = j;
}
}
}
done[closest] = true;
if (closest === dest) {
break;
}
let neighbors = roadsFrom(closest);
for (let nb in neighbors) {
let w = neighbors[nb];
if (!done[nb]) {
if (distance[closest] + w < distance[nb]) {
distance[nb] = distance[closest] + w;
pred[nb] = closest;
}
}
}
}
let traveled = [];
// Done, now print.
i = dest;
if (distance[i] < inf) {
const person = getPersById(Number(i));
traveled.push(person);
let thePath = person.name;
let place = i;
while (place !== source) {
place = pred[place];
if (place !== source) {
const pers = getPersById(Number(place));
traveled.push(pers);
thePath = pers.name + '->' + thePath;
}
}
const p = getPersById(Number(place));
thePath = p.name + '->' + thePath;
const pSource = getPersById(Number(source));
traveled.push(pSource)
console.log("Nombre de degrés de " + pSource.name + "-->" + getPersById(Number(dest)).name + " : " +
distance[i] + ' (' + thePath + ')');
} else {
console.log("Pas de la même famille");
}
return traveled.reverse();
}
let roads = {};
function makeRoad(from, to, length) {
function addRoad(from, to) {
if (!(from in roads)) {
roads[from] = {};
}
roads[from][to] = length;
}
addRoad(from, to);
addRoad(to, from);
}
function makeRoads(start) {
for (let i = 1; i < arguments.length; i += 2) {
makeRoad(start, arguments[i], arguments[i + 1]);
}
}
function roadsFrom(place) {
let found = roads[place];
if (found === undefined) {
print("No place named '" + place + "' found.");
} else {
return found;
}
}
\ No newline at end of file
......@@ -462,78 +462,90 @@ function createJsonGlobal(json){
family.push(dirigeant);
seen[dirigeant.id] = true;
for(let i in dirigeant.parents)
for(let i of dirigeant.parents)
{
let parent = getPersById(dirigeant.parents[i]);
if(!seen[parent.id]){
let parent = getPersById(i);
if(!seen[i]){
family.push(parent);
seen[parent.id] = true;
for(let j in parent.parents){ // grands-parents dirigeant
let grandparent = getPersById(parent.parents[j]);
if(!seen[grandparent.id]){
seen[i] = true;
for(let j of parent.parents){ // grands-parents dirigeant
let grandparent = getPersById(j);
if(!seen[j]){
family.push(grandparent);
seen[grandparent.id] = true;
for(let u in grandparent.children){ // oncles et tantes du dirigeant
let uncle = getPersById(grandparent.children[u]);
if(!seen[uncle.id]){
seen[j] = true;
for(let u of grandparent.children){ // oncles et tantes du dirigeant
let uncle = getPersById(u);
if(!seen[u]){
family.push(uncle);
seen[uncle.id] = true;
for(let j in uncle.children){ // cousins du dirigeant
let nephew = getPersById(uncle.children[j]);
if(!seen[nephew.id]){
family.push(nephew);
seen[nephew.id] = true;
}
seen[u] = true;
for(let jU of uncle.children){ // cousins du dirigeant
let nephew = getPersById(jU);
if(!seen[jU]){ family.push(nephew); seen[jU] = true;}
}
for(let j in uncle.partners){ // femmes et maris des oncles et tantes
let partner = getPersById(uncle.partners[j]);
if(!seen[partner.id]){
family.push(partner);
seen[partner.id] = true;
}
for(let jP of uncle.partners){ // femmes et maris des oncles et tantes
let partner = getPersById(jP);
if(!seen[jP]){ family.push(partner); seen[jP] = true;}
}
}
}
}
}
for(let j in parent.children){ // freres soeurs du dirigeant
let bro = getPersById(parent.children[j]);
if(!seen[bro.id]){
for(let broId of parent.children){ // freres soeurs du dirigeant
let bro = getPersById(broId);
if(!seen[broId]){
family.push(bro);
seen[bro.id] = true;
for(let j in bro.children){ // mari ou femme des freres et soeur du dirigeant
let child = getPersById(bro.children[j]);
if(!seen[child.id]){
family.push(child);
seen[child.id] = true;
}
seen[broId] = true;
for(let nephewId of bro.children){ // neveux et nièces du dirigeant
let nephew = getPersById(nephewId);
if(!seen[nephewId]){ family.push(nephew); seen[nephewId] = true;}
}
for(let j in bro.partners){ // mari ou femme des freres et soeur du dirigeant
let partner = getPersById(bro.partners[j]);
if(!seen[partner.id]){
family.push(partner);
seen[partner.id] = true;
}
for(let j of bro.partners){ // mari ou femme des freres et soeur du dirigeant
let partner = getPersById(j);
if(!seen[j]){ family.push(partner); seen[j] = true;}
}
}
}
}
}
for(let j in dirigeant.children){ //enfants dirigeant
let enfantDirigeant = getPersById(dirigeant.children[j]);
if(!seen[enfantDirigeant.id]){
for(let j of dirigeant.children){ //enfants dirigeant
let enfantDirigeant = getPersById(j);
if(!seen[j]){
family.push(enfantDirigeant);
seen[enfantDirigeant.id] = true;
seen[j] = true;
for(let gchId of enfantDirigeant.children){ //petits enfants dirigeant
let gch = getPersById(gchId);
if(!seen[gchId]){
family.push(gch);
seen[gchId] = true;
for(let spId of gch.partners){ // époux des petits enfants du dirigeant
let sp = getPersById(spId);
if(!seen[gchId]){
family.push(sp);
seen[spId] = true;
}
}
}
}
}
for(let chSpId of enfantDirigeant.partners){ // gendre et bru du dirigeant
let chSp = getPersById(chSpId);
if(!seen[chSpId]){
family.push(chSp);
seen[chSpId] = true;
}
}
}
for(let j in dirigeant.partners){ // mari ou femme du dirigeant
let partner = getPersById(dirigeant.partners[j]);
if(!seen[partner.id]){
for(let j of dirigeant.partners){ // mari ou femme du dirigeant
let partner = getPersById(j);
if(!seen[j]){
family.push(partner);
seen[partner.id] = true;
seen[j] = true;
}
}
// on enlève les liens vers des personnes extérieures au graphe choisi précédemment
let identifiants = [];
for(let p of family){
identifiants.push(p.id);
......@@ -557,6 +569,7 @@ function createJsonGlobal(json){
}
return family;
}
function getMultipleFamily(leaders){
if(leaders.length == 1){
return getFamily(leaders[0]);
......@@ -577,6 +590,8 @@ function createJsonGlobal(json){
bigFamily[i].children = Array.from(set);
set = new Set(bigFamily[i].parents.concat(bigFamily[j].parents));
bigFamily[i].parents = Array.from(set);
set = new Set(bigFamily[i].partners.concat(bigFamily[j].partners));
bigFamily[i].partners = Array.from(set);
bigFamily.splice(j,1);
}
}
......@@ -591,18 +606,17 @@ function createJsonGlobal(json){
if(person1.name === person2.name){
return true;
}
let trouve = false;
if(list.indexOf(person1.id) < 0){
list.push(person1.id)
if(person1.hasOwnProperty('parents')){
for(let idParent of person1.parents){
let parent = getPersById(idParent);
if(list.indexOf(parent.id) < 0 && (parent.hasOwnProperty('parents') || parent.hasOwnProperty('children'))){
if(isInFamily(parent, person2, list)){
relations.push("parent's")
degree++;
return true;
relations.push("parent's");
trouve = true;
return trouve;
}
}
}
......@@ -613,9 +627,9 @@ function createJsonGlobal(json){
let child = getPersById(idChildren);
if(list.indexOf(child.id) < 0 && (child.hasOwnProperty('children') || child.hasOwnProperty('parents'))){
if(isInFamily(child, person2, list)){
degree++;
relations.push("child's")
return true;
relations.push("child's");
trouve = true;
return trouve;
}
}
}
......@@ -625,10 +639,9 @@ function createJsonGlobal(json){
let partner = getPersById(idPartner);
if(list.indexOf(partner.id) < 0 && (partner.hasOwnProperty('parents') || partner.hasOwnProperty('children'))){
if(isInFamily(partner, person2, list)){
degree++;
relations.push("partner's");
return true;
trouve = true;
return trouve;
}
}
......@@ -636,11 +649,12 @@ function createJsonGlobal(json){
}
}
return false;
return trouve;
}
/**
* getter de personne par nom
*/
......@@ -661,6 +675,32 @@ function createJsonGlobal(json){
}
}
}
function listRelations(traveled){
for(let i = 0; i < traveled.length-1; i++){
const pers = traveled[i];
for(let idParent of pers.parents){
const parent = getPersById(idParent);
if(parent.name === traveled[i+1].name){
console.log("parent");
}
}
for(let idChild of pers.children){
const child = getPersById(idChild);
if(child.name === traveled[i+1].name){
console.log("child");
}
}
for(let idPartner of pers.partners){
const partner = getPersById(idPartner);
if(partner.name === traveled[i+1].name){
console.log("partner");
}
}
}
}
/**
* construction du graphe pour le dirigeant donné en paramètre
*/
......@@ -669,18 +709,29 @@ function createJsonGlobal(json){
tree = [];
degree = 0;
relations = [];
roads = {}
if(typeof(leaders) === "object"){
tree = getMultipleFamily(leaders);
console.log(isInFamily(getPersByName(leaders[0]),getPersByName(leaders[1]),new Array()));
relations = relations.reverse().join(" ");
relations = relations.replace("parent's parent's parent's","great-grandparent's");
/* relations = relations.replace("parent's parent's parent's","great-grandparent's");
relations = relations.replace("parent's parent's","grandparent's");
relations = relations.replace("child's child's child's","great-grandchild's");
relations = relations.replace("child's child's","grandchild's");
relations = relations.replace("parent's child's","sibling's");
relations = relations.replace("grandparent's grandchild's", "cousin's")
relations = relations.replace("cousin's parent's", "uncles or aunt 's")*/
const p1 = getPersByName(leaders[0]);
const p2 = getPersByName(leaders[1]);
const traveled = Dijkstra(roads,p1.id + "", p2.id + "");
listRelations(traveled)
}
else if(typeof(leaders) === "string"){
tree = getFamily(leaders);
}
build_graph();
rank();
normalize_layers();
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter