Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "source.h"
- int main(){
- FILE *input = fopen("graph.txt", "r");
- if (input == NULL)
- printf("Cannot open this file \n");
- else{
- roots = (struct root *)realloc((void *)roots, (edges)*sizeof(struct root));
- int step = 0;
- int target = 0;
- char s[1024];
- while (!feof(input)){
- fgets(s, 1024, input);
- if (parse_string(s)) {
- roots = (struct root *)realloc((void *)roots, (edges = edges + 1)*sizeof(struct root));
- roots[edges - 1].start = buff_b;
- roots[edges - 1].end = buff_e;
- roots[edges - 1].v = buff_w;
- if (roots[edges - 1].start + 1 >= dots)
- dots = roots[edges - 1].start + 1;
- if (roots[edges - 1].end + 1 >= dots)
- dots = roots[edges - 1].end + 1;
- }
- }
- fclose(input);
- ostgraph = (struct root*)malloc((dots - 1)*sizeof(struct root));
- //сортировка
- struct root tmp;
- for (int i = 0; i < edges - 1; i++){
- for (int j = 0; j < edges - i - 1; j++){
- if (roots[j].v > roots[j + 1].v){
- tmp.v = roots[j].v;
- tmp.start = roots[j].start;
- tmp.end = roots[j].end;
- roots[j].v = roots[j + 1].v;
- roots[j].start = roots[j + 1].start;
- roots[j].end = roots[j + 1].end;
- roots[j + 1].v = tmp.v;
- roots[j + 1].start = tmp.start;
- roots[j + 1].end = tmp.end;
- }
- }
- }
- printf("Sorted graph:\n\n");
- for (int i = 0; i < edges; i++){
- printf("begin : %d end: %d weight: %d \n", roots[i].start, roots[i].end, roots[i].v);
- }
- //сам алгоритм
- target = 0;
- int ttarget = 0;
- step = 0;
- for (int i = 0; i < edges; i++){
- for (int j = 0; j < dots; j++){
- if (roots[i].end == ostgraph[j].end){
- ttarget = 1;
- }
- if (roots[i].start == ostgraph[j].start){
- target = 1;
- }
- }
- if (target == 0 || ttarget == 0){
- ostgraph[step].start = roots[i].start;
- ostgraph[step].end = roots[i].end;
- ostgraph[step].v = roots[i].v;
- step++;
- }
- if (step == dots - 1)
- continue;
- target = 0;
- ttarget = 0;
- }
- printf("\n\nOur ost graph: \n\n");
- for (int i = 0; i < dots - 1; i++){
- printf("begin : %d end: %d weight: %d \n", ostgraph[i].start, ostgraph[i].end, ostgraph[i].v);
- }
- //графиз
- FILE *inspect = fopen("graphviz.dot", "w+");
- fprintf(inspect, "graph G{\n");
- target = 1;
- for (int i = 0; i < dots - 1; i++){
- fprintf(inspect, " %d -- %d [ label = \"%d\"];\n", ostgraph[i].start, ostgraph[i].end, ostgraph[i].v);
- }
- fprintf(inspect, "}");
- fclose(inspect);
- free(ostgraph);
- free(graph);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment