Work done on knot-detection. Still figuring out how to best organize the data to look for loops.
This commit is contained in:
parent
a7c0a3babe
commit
a05e4e19c0
125
src/knotted.cpp
125
src/knotted.cpp
@ -1,17 +1,120 @@
|
||||
/* +----------------+---------+--------------------------+
|
||||
| Michael Laymon | mal0420 | MichaelLaymon@my.unt.edu |
|
||||
+----------------+---------+--------------------------+
|
||||
| Created 2022-04-17 | Updated 2022-04-27 |
|
||||
+--------------------------+--------------------------+ */
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "graph.hpp"
|
||||
|
||||
/* knotted:
|
||||
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
|
||||
(!THIS MODIFIES THE GRAPH!)
|
||||
@params: none, uses class-internal data
|
||||
@returns:
|
||||
int:
|
||||
true if graph is knotted (deadlock)
|
||||
false if graph is not (no deadlock)
|
||||
*/
|
||||
bool graph::knotted() {
|
||||
return false;
|
||||
|
||||
|
||||
// goal is to create an group of objects that point to each other the same way the graph does and then detect loops
|
||||
/*
|
||||
enum class resourcetype { P, R };
|
||||
|
||||
struct PR {
|
||||
int value;
|
||||
resourcetype type; // {P,R}
|
||||
resourcenum index;
|
||||
vector<struct PR*> in;
|
||||
vector<struct PR*> out;
|
||||
bool visited;
|
||||
};
|
||||
|
||||
void insertPR( struct PR** head,) {
|
||||
struct PR* new_node = new PR; // neclare a new resource
|
||||
new_node->value = new_data;
|
||||
new_node->in.push_back();
|
||||
new_node->out.push_back();
|
||||
new_node->visited = false;
|
||||
}
|
||||
|
||||
void checkKnots () {
|
||||
|
||||
//
|
||||
// convert graph to a list where each item is a resource and has a list of connected resources
|
||||
// ex: 0 (P1) : 0 (P1), 4 (R2)
|
||||
// 1 (P2) : 1 (P2), 3 (R1), 5 (R3)
|
||||
//
|
||||
|
||||
}*/
|
||||
|
||||
|
||||
/* convert adjaceny matrix to adjaceny list */
|
||||
std::vector<std::vector<int>> convert(std::vector<std::vector<int>> x) {
|
||||
std::vector<std::vector<int>> adjacencyList(x.size());
|
||||
for ( int ii = 0; ii < x.size(); ii++ )
|
||||
for ( int jj = 0; jj < x[ii].size(); jj++ )
|
||||
if (x[ii][jj] == 1)
|
||||
adjacencyList[ii].push_back(jj);
|
||||
return adjacencyList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* linked list data structure */
|
||||
struct Vertex {
|
||||
int data; // contents of node
|
||||
struct Vertex* next; // point to next node
|
||||
struct Vertex* prev; // point to prev node
|
||||
bool visited; // flag used to signal visited
|
||||
};
|
||||
|
||||
void pushLL(struct Vertex** list_head, int new_data) {
|
||||
struct Vertex* new_node = new Vertex; // declare a new vertex
|
||||
new_node->data = new_data; // populate the data for the new node
|
||||
new_node->next = (*list_head); // link back to the old list from the new node
|
||||
new_node->prev = (*list_head); // link back to the old list from the new node
|
||||
new_node->visited = false;
|
||||
(*list_head) = new_node; // point list head to the new node
|
||||
}
|
||||
|
||||
bool checkKnot(struct Vertex* v) {
|
||||
|
||||
//vector<vector<int>> a;
|
||||
//vector<vector<int>> AdjacencyList = convert(m);
|
||||
|
||||
std::vector<Vertex*> visited; // set to store the visited nodes
|
||||
while ( v != NULL ) { // loop over the nodes in the linked list if the linked list is not empty
|
||||
/* detect if node has been visited; if has, then there is a knot (and a loop) */
|
||||
if ( v->visited ) // find if this node is visited
|
||||
return true; // immediate return
|
||||
/* if node not visited, visit it and then continue */
|
||||
v->visited = true; // mark node as visited
|
||||
v = v->next; // increment linked list position
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/* knotted:
|
||||
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
|
||||
@params:
|
||||
none, uses class-internal data
|
||||
@returns:
|
||||
bool:
|
||||
true iff graph is knotted (deadlock)
|
||||
false if graph is not knotted (no deadlock)
|
||||
*/
|
||||
bool graph::knotted() {
|
||||
/* Matrix sequel (copy) to preserve original */
|
||||
matrix revolutions = m;
|
||||
struct Vertex* neo = NULL; // neo is the head of the linked list
|
||||
|
||||
for ( int matrix_row = 0; matrix_row < num_processes + num_resources; matrix_row++ ) {
|
||||
for ( int matrix_col = 0; matrix_col < num_processes + num_resources; matrix_col++ ) {
|
||||
if ( matrix_row == matrix_col ) continue; // do not connect processes and resources to themselves
|
||||
if ( m[matrix_row][matrix_col] == 1 ) {
|
||||
pushLL(&neo,matrix_row);
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
Loading…
Reference in New Issue
Block a user