Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Marian POIROT
Our awesome project
Commits
fcf931fd
Commit
fcf931fd
authored
Jun 18, 2021
by
Valentin MEUNIER
Browse files
arbre connexe
parent
18750e67
Changes
4
Hide whitespace changes
Inline
Side-by-side
graph.c
0 → 100644
View file @
fcf931fd
#include "graph.h"
int
**
creer_mat
(
int
n
)
{
int
**
mat
=
malloc
(
n
*
sizeof
(
int
*
));
if
(
!
mat
)
printf
(
"erreur d'allocation de la matrice
\n
"
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
mat
[
i
]
=
malloc
(
n
*
sizeof
(
int
));
if
(
!
mat
[
i
])
printf
(
"erreur d'allocation de la ligne %d
\n
"
,
i
);
}
return
mat
;
}
void
remplir_mat
(
int
**
mat
,
int
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
i
;
j
++
)
mat
[
i
][
j
]
=
rand
()
%
8
;
}
}
void
affiche_graph
(
int
**
mat
,
int
n
)
{
FILE
*
fichier
;
fichier
=
fopen
(
"graph1.dot"
,
"w"
);
if
(
fichier
==
NULL
)
printf
(
"echec de louverture du fichier
\n
"
);
else
fprintf
(
fichier
,
"graph Nom{
\n
"
);
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
fprintf
(
fichier
,
"%d"
,
i
);
fprintf
(
fichier
,
";"
);
for
(
int
j
=
0
;
j
<
i
;
j
++
)
{
if
(
mat
[
i
][
j
]
==
1
)
{
fprintf
(
fichier
,
"%d"
,
i
);
fprintf
(
fichier
,
"--"
);
fprintf
(
fichier
,
"%d"
,
j
);
fprintf
(
fichier
,
";"
);
}
}
}
fprintf
(
fichier
,
"}"
);
fclose
(
fichier
);
}
void
partition_connexe
(
partition_t
*
part
,
int
**
mat
,
int
n
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
for
(
int
j
=
0
;
j
<
i
;
j
++
)
if
(
mat
[
i
][
j
]
==
1
)
fusion
(
i
,
j
,
part
);
}
int
main
()
{
srand
(
time
(
0
));
int
**
mat
=
NULL
;
mat
=
creer_mat
(
10
);
remplir_mat
(
mat
,
10
);
partition_t
*
part
=
creer
(
10
);
/* for (int i=0; i<10 ;i++)
{
for (int j=0; j<i; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
*/
affiche_graph
(
mat
,
10
);
partition_connexe
(
part
,
mat
,
10
);
affiche_part
(
part
,
10
);
system
(
"dot -Tpng graph1.dot -o graph1.png"
);
system
(
"dot -Tpng graph_part.dot -o graph_part.png"
);
system
(
"display graph1.png &"
);
system
(
"display graph_part.png &"
);
return
0
;
}
graph.h
0 → 100644
View file @
fcf931fd
#ifndef _gard_graph_
#define _gard_graph_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include <time.h>
#include "partition.h"
int
**
creer_mat
(
int
n
);
void
remplir_mat
(
int
**
mat
,
int
n
);
void
affiche_graph
(
int
**
mat
,
int
n
);
void
partition_connexe
(
partition_t
*
,
int
**
,
int
);
int
main
();
#endif
partition.c
View file @
fcf931fd
...
...
@@ -23,16 +23,20 @@ int recuperer_classe(int indice, partition_t * part)
void
fusion
(
int
indice1
,
int
indice2
,
partition_t
*
part
)
{
int
c1
=
recuperer_classe
(
indice1
,
part
);
int
c2
=
recuperer_classe
(
indice2
,
part
);
if
(
part
[
c1
].
taille
>
part
[
c2
].
taille
)
part
[
c2
].
parent
=
c1
;
else
if
(
part
[
c1
].
taille
<
part
[
c2
].
taille
)
part
[
c1
].
parent
=
c2
;
else
if
(
c1
!=
c2
)
{
part
[
c1
].
parent
=
c2
;
part
[
c2
].
taille
+=
1
;
if
(
part
[
c1
].
taille
>
part
[
c2
].
taille
)
part
[
c2
].
parent
=
c1
;
else
if
(
part
[
c1
].
taille
<
part
[
c2
].
taille
)
part
[
c1
].
parent
=
c2
;
else
{
part
[
c1
].
parent
=
c2
;
part
[
c2
].
taille
+=
1
;
}
}
}
...
...
@@ -57,10 +61,10 @@ void lister_partition(partition_t * part,int n, int * nbclasse,int * tab)
*
nbclasse
=
j
;
}
void
graph
(
partition_t
*
part
,
int
n
)
void
affiche_part
(
partition_t
*
part
,
int
n
)
{
FILE
*
fichier
;
fichier
=
fopen
(
"graph.dot"
,
"w"
);
fichier
=
fopen
(
"graph
_part
.dot"
,
"w"
);
if
(
fichier
==
NULL
)
printf
(
"echec de louverture du fichier
\n
"
);
else
...
...
@@ -74,16 +78,5 @@ void graph(partition_t * part, int n)
}
fprintf
(
fichier
,
"}"
);
fclose
(
fichier
);
system
(
"dot -Tpng graph.dot -o graph.png"
);
system
(
"display graph.png"
);
}
int
main
()
{
partition_t
*
part
=
creer
(
10
);
fusion
(
0
,
1
,
part
);
fusion
(
1
,
2
,
part
);
fusion
(
3
,
4
,
part
);
fusion
(
2
,
4
,
part
);
graph
(
part
,
10
);
}
partition.h
View file @
fcf931fd
...
...
@@ -19,6 +19,6 @@ int recuperer_classe(int indice, partition_t * part);
void
fusion
(
int
indice1
,
int
indice2
,
partition_t
*
part
);
void
lister_classe_naif
(
partition_t
*
part
,
int
etiquette
,
int
n
);
void
lister_partition
(
partition_t
*
part
,
int
n
,
int
*
,
int
*
);
void
graph
(
partition_t
*
part
,
int
);
void
affiche_part
(
partition_t
*
part
,
int
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment