Skip to content
Snippets Groups Projects
Commit 428f1706 authored by Filip Štěpánek's avatar Filip Štěpánek
Browse files

Working but wrong algo lol

parent 6a69350f
No related branches found
No related tags found
No related merge requests found
import networkx as nx
import matplotlib.pyplot as plt
file1 = open('pub03.in', 'r')
headerline = file1.readline()
print(headerline)
header = headerline.split()
factoryCount = int(header[0])
convCount = int(header[1])
mainFactory = int(header[2])
G = nx.DiGraph()
for i in range(factoryCount):
G.add_node(i)
edges = []
for i in range(convCount):
edgeText = headerline = file1.readline()
edge = edgeText.split()
G.add_edge(int(edge[0]),int(edge[1]))
G.add_edges_from(edges)
pos = nx.spring_layout(G, iterations=20, seed=39775)
nx.draw(G, pos=pos, with_labels=True, font_weight='bold', node_size=factoryCount)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
#nx.draw(G.subgraph(mainFactory), pos=pos, node_color='red', font_color='green')
plt.show()
\ No newline at end of file
......@@ -9,8 +9,10 @@ struct Cluster;
struct Node {
int index;
std::list<Node *> neighbors;
std::list<Node *> invertedNeighbors;
std::list<Node *> directNeighbors;
int distance = INT_MAX;
int reconfigurations = INT_MAX;
int depth = 0;
bool visited = false;
Node *predecesor = nullptr;
Cluster *cluster = nullptr;
......@@ -26,7 +28,7 @@ struct Cluster {
class MyCompare {
public:
bool operator()(Node *a, Node *b) {
return (a->distance) > (b->distance);
return (a->reconfigurations) > (b->reconfigurations);
}
};
......@@ -62,6 +64,7 @@ int main() {
graph[source].neighbors.push_back(&graph[dest]);
graph[dest].invertedNeighbors.push_back(&graph[source]);
graph[source].directNeighbors.push_back(&graph[dest]);
graph[dest].directNeighbors.push_back(&graph[source]);
......@@ -72,7 +75,13 @@ int main() {
Kosarajus();
clusters.sort([](const Cluster &a, const Cluster &b) -> bool {
return a.distance > b.distance;
if (a.nearestFactory->depth > b.nearestFactory->depth) return true;
if (b.nearestFactory->depth > a.nearestFactory->depth) return false;
if (a.nearestFactory->reconfigurations > b.nearestFactory->reconfigurations) return true;
if (b.nearestFactory->reconfigurations > a.nearestFactory->reconfigurations) return false;
return false;
});
int re = 0;
......@@ -83,9 +92,12 @@ int main() {
Cluster *cluster = &(*it);
do {
Node *me = cluster->nearestFactory;
Node *me = cluster->nearestFactory;
while (me->predecesor != nullptr) {
Node *predecesor = me->predecesor;
me->cluster->visited = true;
if (std::find(predecesor->neighbors.begin(), predecesor->neighbors.end(), me) !=
predecesor->neighbors.end()) {
......@@ -97,9 +109,9 @@ int main() {
};
cluster->visited = true;
cluster = predecesor->cluster;
} while (cluster->nearestFactory->predecesor != nullptr);
me = predecesor;
}
}
std::cout << re << std::endl;
......@@ -189,8 +201,8 @@ void DFSUtil(Node *v, Cluster *pCluster) {
pCluster->nodes.push_back(v);
v->cluster = pCluster;
if (pCluster->distance > v->distance) {
pCluster->distance = v->distance;
if (pCluster->distance > v->reconfigurations) {
pCluster->distance = v->reconfigurations;
pCluster->nearestFactory = v;
}
......@@ -232,12 +244,11 @@ void BFS(Node *src) {
// Create a queue for BFS
std::list<Node *> queue;
std::list<int> levelQueue;
// Mark the current node as visited and enqueue it
src->visited = true;
src->reconfigurations = 0;
queue.push_back(src);
levelQueue.push_back(0);
// 'i' will be used to get all adjacent
// vertices of a vertex
......@@ -247,20 +258,35 @@ void BFS(Node *src) {
while (!queue.empty()) {
// Dequeue a vertex from queue and print it
src = queue.front();
src->distance = levelQueue.front();
std::cout << src->index << "-" << src->distance << std::endl;
std::cout << src->index << "-" << src->reconfigurations << std::endl;
queue.pop_front();
levelQueue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (i = src->directNeighbors.begin(); i != src->directNeighbors.end(); ++i) {
for (i = src->neighbors.begin(); i != src->neighbors.end(); ++i) {
if (!(*i)->visited) {
(*i)->visited = true;
(*i)->predecesor = src;
(*i)->reconfigurations = src->reconfigurations;
(*i)->depth = src->depth + 1;
queue.push_back(*i);
} else if ((*i)->reconfigurations > src->reconfigurations) {
(*i)->predecesor = src;
(*i)->reconfigurations = src->reconfigurations;
}
}
for (i = src->invertedNeighbors.begin(); i != src->invertedNeighbors.end(); ++i) {
if (!(*i)->visited) {
(*i)->visited = true;
(*i)->predecesor = src;
(*i)->reconfigurations = src->reconfigurations + 1;
(*i)->depth = src->depth + 1;
queue.push_back(*i);
levelQueue.push_back(level);
} else if ((*i)->reconfigurations > src->reconfigurations + 1) {
(*i)->predecesor = src;
(*i)->reconfigurations = src->reconfigurations + 1;
}
}
level++;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment