Skip to content
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
21 changes: 21 additions & 0 deletions Sherstnev_EE/lab4/include/Algoritm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include "Graph.h"
#include "Dheap.h"
#include "priority_queue.h"

using namespace std;

class CompleteVertex : public VertexLabel
{
public:
int v;
CompleteVertex(int v, float dist);
};


class Algoritm
{
public:
static void Dijkstra(Graph*&, int, float*&,int*&);
};

42 changes: 42 additions & 0 deletions Sherstnev_EE/lab4/include/Dheap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <iostream>
#include <cmath>

#define max_heap 10000

using namespace std;


class VertexLabel
{
public:

float label;

};



class Dheap
{
private:

VertexLabel** labels;
int d;
int lastidx;
public:
Dheap(int);
~Dheap();
void transpose(int, int);
void add(VertexLabel*&);
void addSet(VertexLabel **key, int num);
VertexLabel* erase(int);
void surfacing(int);
void sinking(int);
void spudding();
int isFull();
int isEmpty();
int minChild(int);
};

40 changes: 40 additions & 0 deletions Sherstnev_EE/lab4/include/Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

class WeightedEdge
{
public:
int ne; //nachalo
int ke; //konec
float we; //ves
WeightedEdge(int ne, int ke, float we);
};

class Graph
{
private:
int n; // vers
int m; // rebra
int current;
WeightedEdge** edges;
int* vertices;
private:
void generateVertices(int &N, int &K);
float generateWeight(float minRange, float maxRange);
void cleaner();
int findEdge(int N, int K);
public:
Graph(int n, int m);
void generateGraph(float minRange, float maxRange);

void Push(int, int, float);
void ConnectCheck();
int GetNumberofVertex();
int GetNumberofEdges();
int GetCursor();
WeightedEdge* GetEdge(int);
WeightedEdge** GetEdgeSet();
float GetWeight(int, int);
void Print();
~Graph();
};

34 changes: 34 additions & 0 deletions Sherstnev_EE/lab4/include/priority_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
#include "Dheap.h"


class priority_queue
{
public:
priority_queue() {};

virtual void push(VertexLabel*&key) = 0;
virtual VertexLabel* pop() = 0;
virtual void refresh() = 0;
virtual int isFull() = 0;
virtual int isEmpty() = 0;
};


class DheapBasedPriorityQueue : public priority_queue
{
protected:
Dheap * heap;
public:
DheapBasedPriorityQueue(int d = 4);
DheapBasedPriorityQueue(VertexLabel **keys, int num, int d = 4);
~DheapBasedPriorityQueue();

virtual void push(VertexLabel *&key);
virtual VertexLabel* pop();
virtual void refresh();
virtual int isFull();
virtual int isEmpty();
};


116 changes: 116 additions & 0 deletions Sherstnev_EE/lab4/samples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <iostream>
#include "graph.h"
#include "Algoritm.h"
#include <cfloat>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
Graph *graph;
int n;
int m;
int s;
try {
int N;
int K;
float weight;
int flag;
std::cout << "vvedite chislo vershin: ";
std::cin >> n;
std::cout << "vvedite chislo reber: ";
std::cin >> m;
std::cout << "vvedite startovuy vershinu: ";
std::cin >> s;
graph = new Graph(n, m);
int cur = graph->GetCursor();
std::cout << "\n 1 - Graph will fills automaticly. \n 2 - Graph will fill manually \n";
std::cin >> flag;
std::cout << "\n";
switch (flag)
{
case 1: {
graph->generateGraph(0, 50);
break;
}
case 2: {
while (cur < m) {
std::cout << "vvedite pervuy tochku: ";
std::cin >> N;
if (N == -1) break;
std::cout << "vvedite vtoruy tochku: ";
std::cin >> K;
if (K == -1) break;
std::cout << "vvedite ves rebra: ";
std::cin >> weight;
std::cout << "\n";
graph->Push(N, K, weight);
cur = graph->GetCursor();
}
break;
}
}

}


catch (...) {
return -1;
}

try {
graph->ConnectCheck();
}

catch (...) {
return -1;
}
graph->Print();
std::cout << endl;

float *dist;
int *up;
try {
Algoritm::Dijkstra(graph, s, dist, up);
}
catch (...) {
return -2;
}

remove("ways.txt");
ofstream output("ways.txt");
output.precision(2);

output << n << ' ' << m << endl;
output << s << endl;

m = graph->GetCursor();
WeightedEdge* edge;
for (int j = 0; j < m; j++) {
edge = graph->GetEdge(j);
output << edge->ne << ' ' << edge->ke << ' ' << edge->we << endl;
}

std::cout << "Distanses : \n";

for (int i = 0; i < n; i++)
if (dist[i] != FLT_MAX)
std::cout << dist[i] << ' ';
else
std::cout << "inf" << ' ';

std::cout << "\nPassed Vertexes: \n";

for (int i = 0; i < n; i++)
std::cout << up[i] << ' ';


getchar();
output.close();
delete graph;
delete[]dist;
delete[]up;
getchar();
return 0;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading