Skip to content
Snippets Groups Projects
Commit 526c63d0 authored by MAAZOUZ Ilyas's avatar MAAZOUZ Ilyas
Browse files

hh

parent c0f2d395
No related branches found
No related tags found
No related merge requests found
Showing
with 145 additions and 29 deletions
File added
File added
File added
File added
lgorithme 3 : DFS(G)
Données : graphe G
début
pour chaque s sommet de G
faire
couleur[s] ← BLANC ;
π[s] ← NULL ;
fin
temps ← 0 ;
pour chaque s sommet de G
faire
si couleur[s] = BLANC
alors
Visiter(s) ;
fin
fin
fin
Algorithme 4 : Visiter(u)
début
couleur[u] ← GRIS ;
d[u] ← temps ← temps + 1 ;
pour chaque v voisin de u
faire
si couleur[v ] = BLANC
alors
π[v ] ← u ;
Visiter(v ) ;
fin
fin
couleur[u] ← NOIR ;
f [u] ← temps ← temps + 1 ;
fin
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
c Commentaire
p cnf 3 4
1 -2 0
-1 3 0
1 3 0
-2 -3 0
......@@ -21,25 +21,19 @@ public class App {
ParcoursenProfondeur PP = new ParcoursenProfondeur(graph1);
PP.parcours(graph1);
int i = 0;
Transpose TT = new Transpose(graph1, 5);
for (Digraph.Arc e : TT.Transposed.arcs()) {
Transpose TT = new Transpose(graph1);
for (Digraph.Arc e : TT.Trans.arcs()) {
System.out.println(e.source() + "--->" + e.destination());
}
ParcoursenProfondeur parcoured = new ParcoursenProfondeur(TT.Transposed);
ParcoursenProfondeur parcoured = new ParcoursenProfondeur(TT.Trans);
parcoured.parcours(TT.Transposed);
System.out.println("----------------------------------------");
for (i = 0; i < graph1.cardinal; i++) {
System.out.println(" Sommet : " + i + "Date de sa debut est :" + parcoured.dated.get(i));
System.out.println(" Sommet : " + i + "Date de sa fin est :" + parcoured.datef.get(i));
System.out.println(" Sommet : " + i + "Parent :" + parcoured.parent.get(i));
ComposanteConnexe scc = new ComposanteConnexe(graph1);
scc.printComponents();
}}
}
for (int h: graph1.vertices())
System.out.println(h);
//Collections.reverse(PP.CFC);
System.out.println(parcoured.reverse());
}
}
/*import java.util.List;
import java.lang.Math.*;
import java.util.Collections;
import java.util.*;
import java.util.stream.Collectors;
public class ComposanteConnexe {
Digraph LastTranspose;
List<Integer> CFC;
int x;
public ComposanteConnexe(Transpose T,ParcoursenProfondeur P){
this.LastTranspose = T.afficher();
Collections.reverse(this.CFC);
private Digraph graph;
private ArrayList<ArrayList<Integer>> components;
public ComposanteConnexe(Digraph graph) {
this.graph = graph;
this.components = new ArrayList<>();
computeSCC();
}
public void Composantes(ParcoursenProfondeur P){
for(x in this.CFC){
private void computeSCC() {
// Étape 1: Effectuez un premier parcours DFS et empilez les sommets dans un ordre topologique
Stack<Integer> stack = new Stack<>();
boolean[] visited = new boolean[graph.order()];
for (Integer vertex : graph.vertices()) {
if (!visited[vertex]) {
dfsFirstPass(vertex, visited, stack);
}
}
// Étape 2: Transposez le graphe
Digraph transposedGraph = transposeGraph(graph);
// Étape 3: Effectuez un deuxième parcours DFS en utilisant l'ordre topologique inversé
Arrays.fill(visited, false);
while (!stack.isEmpty()) {
int vertex = stack.pop();
if (!visited[vertex]) {
ArrayList<Integer> component = new ArrayList<>();
dfsSecondPass(transposedGraph, vertex, visited, component);
components.add(component);
}
}
}
private void dfsFirstPass(int vertex, boolean[] visited, Stack<Integer> stack) {
visited[vertex] = true;
for (Digraph.Arc arc : graph.outIncident(vertex)) {
int neighbor = arc.destination();
if (!visited[neighbor]) {
dfsFirstPass(neighbor, visited, stack);
}
}
stack.push(vertex);
}
// Méthode pour le deuxième passage DFS (Étape 3)
private void dfsSecondPass(Digraph graph, int vertex, boolean[] visited, ArrayList<Integer> component) {
visited[vertex] = true;// Marquez le sommet comme visité.
component.add(vertex);// Ajoutez ce sommet à la composante actuelle.
// Parcourez tous les arcs sortants du sommet.
for (Digraph.Arc arc : graph.outIncident(vertex)) {
int neighbor = arc.destination();// Obtenez le voisin suivant
if (!visited[neighbor]) {
// Si le voisin n'a pas encore été visité, effectuez un DFS récursif.
dfsSecondPass(graph, neighbor, visited, component);
}
}
}
*/
private Digraph transposeGraph(Digraph graph) {
Digraph transposedGraph = new Digraph(graph.order()); // Créez un nouveau graphe avec le même nombre de sommets.
// Parcourez tous les arcs du graphe d'origine.
for (Digraph.Arc arc : graph.arcs()) {
// Inversez la direction de l'arc en ajoutant un nouvel arc dans le graphe transposé,
// en échangeant la source et la destination.
transposedGraph.addArc(arc.destination(), arc.source());
}
return transposedGraph;// Retournez le graphe transposé avec les arêtes inversées.
}
// Getters pour obtenir les SCC
public ArrayList<ArrayList<Integer>> getComponents() {
return components;
}
// Getter pour obtenir le nombre de SCC
public int getNumberOfComponents() {
return components.size();
}
public void printComponents() {
if (components.isEmpty()) {
System.out.println("No strongly connected components found.");
return; // Exit the method
}
for (int i = 0; i < components.size(); i++) {
ArrayList<Integer> component = components.get(i);
// Use Java streams to join the vertices in the component with commas
String componentStr = component.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
System.out.println("Component " + (i + 1) + ": " + componentStr);
}
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ public class Digraph {
// Mise a Jour : Cardinal cardinal = size
public Digraph(int size) {
cardinal = size; //Mise ajour cardinal = siz+1
incidency = new ArrayList<LinkedList<Arc>>(size+1);
incidency = new ArrayList<LinkedList<Arc>>(size);
for (int i = 0; i < cardinal; i++) {
incidency.add(i, new LinkedList<Arc>());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment