1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
QList<QList<Def_Item>> Graphic::connectedComponent(QList<Def_Item> v, QList<Def_Relation> w)
{
QList<QList<Def_Item>> liste;
/* Algorithme:
* DFS (graphe G, sommet s)
* {
* Marquer(s);
* POUR CHAQUE élément s_fils de Voisins(s) FAIRE
* SI NonMarqué(s_fils) ALORS
* DFS(G,s_fils);
* FIN-SI
* FIN-POUR
* }
* */
// Initialisation: on remet tous les noeuds non colorés
foreach (Def_Relation i, w){
i.get_objet1().set_pointe(false);
i.get_objet2().set_pointe(false);
}
// on commence le parcours: on colore l'élément 1 et on l'ajoute à la liste
Def_Item item = w[0].get_objet1();
item.set_pointe(true);
foreach (Def_Item i,v){
if (i.get_pointe()==false){
liste.append(parcours(i,v,w));
}
}
return liste;
}
QList<Def_Item> Graphic::parcours(Def_Item item, QList<Def_Item> v, QList<Def_Relation> w) // Recherche de la liste rapportée à chaque item regardé
{
item.set_pointe(true);
// on recherche les éléments qui lui sont liés et on les colore:
foreach (Def_Relation i,w){
if ((i.get_objet1().label()==item.label()) && (i.get_objet1().get_pointe()==false)){
parcours(i.get_objet1(),v,w);
}
if ((i.get_objet2().label()==item.label()) && (i.get_objet2().get_pointe()==false)){
parcours(i.get_objet2(),v,w);
}
}
QList<Def_Item> sortie;
foreach (Def_Item i,v){
if (i.get_pointe()==true){
sortie.append(i);
}
}
return sortie;
} |
Partager