Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kruskal prim #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
#include "add.h"
#include <iostream>
#include "kruskal.h"
#include "prim.h"
#include "graph.h"

int main() {
std::cout << "2 + 2 = " << add(2, 2) << std::endl;
Graph g, g2;
Graph ostov;
char q1[] = "example.txt";
char q2[] = "example2.txt";

// ������ ���� �� ������ ���������
g = create_graph(q2);
std::cout << "Created graph:" << std::endl << std::endl;
Print(&g);
// �������� ��������
Kruskal(&g, &ostov);
std::cout << "" << std::endl;
std::cout << "Minimum spanning tree of the graph:" << std::endl << std::endl;
Print(&ostov);
std::cout << "" << std::endl;

// ������������ ����������� ����� � ������

char graph1[] = "C:\\Users\\������\\Desktop\\graph1.gv";
char ostov1[] = "C:\\Users\\������\\Desktop\\ostov1.gv";
char g_and_o[] = "C:\\Users\\������\\Desktop\\graph_and_ostov_1.gv";

graph_to_gv(&g, graph1);
ostov_to_gv(&ostov, ostov1);
graph_and_ostov_to_gv(&g, &ostov, g_and_o);

// ������ ���� �� ������� ���������
g2 = Create_graph(q1);
std::cout << "Created graph:" << std::endl << std::endl;
Print(&g2);
// �������� �����
Prim(&g2, &ostov);
std::cout << "" << std::endl;
std::cout << "Minimum spanning tree of the graph:" << std::endl << std::endl;
Print(&ostov);

// ������������ ����������� ����� � ������

char graph2[] = "C:\\Users\\������\\Desktop\\graph2.gv";
char ostov2[] = "C:\\Users\\������\\Desktop\\ostov2.gv";
char g_and_o_2[] = "C:\\Users\\������\\Desktop\\graph_and_ostov_2.gv";

graph_to_gv(&g2, graph2);
ostov_to_gv(&ostov, ostov2);
graph_and_ostov_to_gv(&g2, &ostov, g_and_o_2);

return 0;
}
6 changes: 6 additions & 0 deletions example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
0 5 2 0 7
5 0 0 8 1
2 0 0 5 4
0 8 5 0 8
7 1 4 8 0
6 changes: 6 additions & 0 deletions example2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5 8
3 1 5 2 2 4 7
3 0 5 3 8 4 1
3 0 2 3 5 4 4
3 1 8 2 5 4 8
4 0 7 1 1 2 4 3 8
21 changes: 21 additions & 0 deletions graph1.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
graph ostov {
rankdir=LR;
node[shape=circle];
edge[color=black];
edge[label=8];
B -- D;
edge[label=8];
D -- E;
edge[label=7];
A -- E;
edge[label=5];
A -- B;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
edge[label=2];
A -- C;
edge[label=1];
B -- E;
}
21 changes: 21 additions & 0 deletions graph2.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
graph ostov {
rankdir=LR;
node[shape=circle];
edge[color=black];
edge[label=8];
B -- D;
edge[label=8];
D -- E;
edge[label=7];
A -- E;
edge[label=5];
A -- B;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
edge[label=2];
A -- C;
edge[label=1];
B -- E;
}
30 changes: 30 additions & 0 deletions graph_and_ostov_1.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
graph ostov {
rankdir=LR;
node[shape=doublecircle];
edge[color=black];
edge[label=8];
B -- D;
edge[label=8];
D -- E;
edge[label=7];
A -- E;
edge[label=5];
A -- B;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
edge[label=2];
A -- C;
edge[label=1];
B -- E;
edge[color=green];
edge[label=1];
B -- E;
edge[label=2];
A -- C;
edge[label=4];
C -- E;
edge[label=5];
C -- D;
}
30 changes: 30 additions & 0 deletions graph_and_ostov_2.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
graph ostov {
rankdir=LR;
node[shape=doublecircle];
edge[color=black];
edge[label=8];
B -- D;
edge[label=8];
D -- E;
edge[label=7];
A -- E;
edge[label=5];
A -- B;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
edge[label=2];
A -- C;
edge[label=1];
B -- E;
edge[color=green];
edge[label=1];
E -- B;
edge[label=2];
A -- C;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
}
Binary file added images_with_visualization/app.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/graph1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/graph2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/graph_and_ostov_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/graph_and_ostov_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/ostov1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images_with_visualization/ostov2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions include/add.h

This file was deleted.

59 changes: 59 additions & 0 deletions include/d_heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef INCLUDE_D_HEAP_H_
#define INCLUDE_D_HEAP_H_

#include <iostream>
#define MAXsize 1000

struct Data {
int priority;
};

class D_Heap {
protected:
Data **key;
int d;
int size;

public:
explicit D_Heap(int d);
D_Heap(const D_Heap &heap);
~D_Heap();

int Child(int i);
int Parent(int i);
int isFull();
int isEmpty();

void insert(Data **i);
void insert_group(Data **keys, int num);
Data* erase();
Data* erase(int i);
void transposition(int i, int j);
void surfacing(int i);
void immersion(int i);
void hilling();

int min_Child(int i);
};

/*----------------------------------------------------*/
/* ������������ ������� �� 2-���� */
/*----------------------------------------------------*/

class PriorityQueue {
protected:
D_Heap *heap;
public:
explicit PriorityQueue(int d);
PriorityQueue(Data **keys, int num, int d);
~PriorityQueue();
int isFull();
int isEmpty();

void add(Data **key);
Data* pop(int i);
Data* pop_min();
void update();
};

#endif // INCLUDE_D_HEAP_H_
18 changes: 18 additions & 0 deletions include/disjoint_sets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef INCLUDE_DISJOINT_SETS_H_
#define INCLUDE_DISJOINT_SETS_H_

class Disjoint_Sets {
public:
int size;
int *parent;
int *rank;

explicit Disjoint_Sets(int _size);
~Disjoint_Sets();
void make_set(int x);
void union_sets(int x, int y);
int find_set(int x);
void clear();
};

#endif // INCLUDE_DISJOINT_SETS_H_
31 changes: 31 additions & 0 deletions include/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef INCLUDE_GRAPH_H_
#define INCLUDE_GRAPH_H_
#include <limits>

const char ASCII[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int inf = std::numeric_limits<int>::infinity();
typedef int *pInt;

struct edge {
char x;
char y;
int weight;
};

struct Graph {
int N;
int edge_N;
edge* edges;
};

void make_edge(char, char, int, edge*);
void make_graph(int, int, edge*, Graph*);
Graph Create_graph(char *q);
Graph create_graph(char *q);
void Print(Graph *g);

void graph_and_ostov_to_gv(Graph* graph, Graph* ostov, char* name);
void graph_to_gv(Graph* graph, char* name);
void ostov_to_gv(Graph* ostov, char* name);

#endif // INCLUDE_GRAPH_H_
7 changes: 7 additions & 0 deletions include/kruskal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef INCLUDE_KRUSKAL_H_
#define INCLUDE_KRUSKAL_H_
#include "graph.h"

void Kruskal(Graph*, Graph*);

#endif // INCLUDE_KRUSKAL_H_
19 changes: 19 additions & 0 deletions include/prim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef INCLUDE_PRIM_H_
#define INCLUDE_PRIM_H_
#include "graph.h"
#include"d_heap.h"
#include <vector>

const int INF = 10000000;
class Prim_Data : public Data {
public:
int ver;
Prim_Data(int ver, int weight) {
this->ver = ver;
priority = weight;
}
};

void Prim(Graph*, Graph*);

#endif // INCLUDE_PRIM_H_
13 changes: 13 additions & 0 deletions ostov1.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
graph ostov {
rankdir=LR;
node[shape=doublecircle];
edge[color=green];
edge[label=1];
B -- E;
edge[label=2];
A -- C;
edge[label=4];
C -- E;
edge[label=5];
C -- D;
}
13 changes: 13 additions & 0 deletions ostov2.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
graph ostov {
rankdir=LR;
node[shape=doublecircle];
edge[color=green];
edge[label=1];
E -- B;
edge[label=2];
A -- C;
edge[label=5];
C -- D;
edge[label=4];
C -- E;
}
Binary file added otchet/Otchet_lab_5.doc
Binary file not shown.
3 changes: 0 additions & 3 deletions src/add.cpp

This file was deleted.

Loading