TD n°13 Graphes : Positionnement (x,y) des sommets

Transcription

TD n°13 Graphes : Positionnement (x,y) des sommets
TD n°13
Graphes : Positionnement (x,y) des sommets de l'arborescence
1_ Arborescence d'un graphe : généralité
–
std::multimap : sesArcsDsGraphe : {(a,d), (a,b), (b,c), (c,d), (d,b)}
saRacine
a
b
a
d
b
c
b
b
d
d
e
e
a
a
b
b
a
b
b
b
c
a
b
b
d
b
c
d
b
c
d
e
Pour la multimap sonArborescence (Père, Fils)
Pour la map sesSommetsMarques (Fils, Père)
b
a
b
c
a
b
b
c
d
a
b
b
b
c
d
a
b
b
2_ Programmation C++ : Positionnement des Sommets.
Définition de valeurX
void GrapheArbores::valuerX(Sommet & s, unsigned int niveau)
{
modifierSommetsPourX(s, niveau);
std::multimap<Sommet, Sommet>::iterator it;
for (it = sonArborescence.lower_bound(s);
it != sonArborescence.upper_bound(s);
it++)
{
valuerX((*it).second, niveau+1);
}
}
Définition de valeurY
double GrapheArbores::valuerY(Sommet & s,int & rg)
{
double positionY = -1;
std::multimap<Sommet, Sommet>::iterator it;
if (sonArborescence.lower_bound(s) ==
sonArborescence.upper_bound(s))
{
positionY = rg++;
}
else
{
double sommeY = 0;
int nbFils;
for (it = sonArborescence.lower_bound(s), nbFils=0;
it != sonArborescence.upper_bound(s), nbFils++;
it++)
{
Sommet unFils = (*it).second;
sommeY += valuerY(unFils, rg);
}//fin for
positionY = sommeY / nbFils;
}//fin else
modifierSommetsPourY(s, positionY);
return positionY;
}
Définition de peuplerSommet
void GrapheArbores::peuplerSommet()
{
std::multimap<Sommet, Sommet>::iterator it;
for (it = sonArborescence.begin();
it != sonArborescence.end();
it++)
{
Sommet s=(*it).first; // on ne peut pas "toucher" à la clé, on utilise donc une
variable.
sesSommets.insert(std::make_pair(s.getSonID(),s));
sesSommets.insert(std::make_pair((*it).second.getSonID(),(*it).second));
}
}
Annexe TP
void GrapheArbores::valuerX(Sommet & s, unsigned int niveau)
{
modifierSommetsPourX(s, niveau);
std::multimap<Sommet, Sommet>::iterator it;
for (it = sonArborescence.lower_bound(s);
it != sonArborescence.upper_bound(s);
it++)
{
valuerX((*it).second, niveau+1);
}
}
void GrapheArbores::modifierSommetsPourX(Sommet s, int niv)
{
// c'est sesSommets qui s'occupe des coordonnées x, y
std::map<std::string , Sommet>::iterator it;
it = sesSommets.find(s.getSonID());
if (it!=sesSommets.end())
{
(*it).second.modif_sonX(niv);
}
}
double GrapheArbores::valuerY(Sommet & s,int & rg)
{
double positionY = -1;
std::multimap<Sommet, Sommet>::iterator it;
if (sonArborescence.lower_bound(s) ==
sonArborescence.upper_bound(s))
{
positionY = rg++;
}
else
{
double sommeY = 0;
int nbFils;
for (it = sonArborescence.lower_bound(s), nbFils=0;
it != sonArborescence.upper_bound(s), nbFils++;
it++)
{
Sommet unFils = (*it).second;
sommeY += valuerY(unFils, rg);
}//fin for
positionY = sommeY / nbFils;
}//fin else
modifierSommetsPourY(s, positionY);
return positionY;
}
void GrapheArbores::modifierSommetsPourY(Sommet & s, double & niv)
{
// c'est sesSommets qui s'occupe des coordonnées x, y
std::map<std::string , Sommet>::iterator it;
it = sesSommets.find(s.getSonID());
if (it!=sesSommets.end())
{
(*it).second.modif_sonY(niv);
}
}
void GrapheArbores::positionnerSommets()
{
peuplerSommet();
valuerX(*saRacine, 0);
int rangFeuille = 0;
valuerY(*saRacine,rangFeuille);
}
void GrapheArbores::peuplerSommet()
{
std::multimap<Sommet, Sommet>::iterator it;
for (it = sonArborescence.begin();
it != sonArborescence.end();
it++)
{
Sommet s=(*it).first; // on ne peut pas "toucher" à la clé, on utilise donc une
variable.
sesSommets.insert(std::make_pair(s.getSonID(),s));
sesSommets.insert(std::make_pair((*it).second.getSonID(),(*it).second));
}
}