Commit 55fd8f1b authored by Marvin Großer's avatar Marvin Großer
Browse files

rev

parent b28b8ea8
build/Debug/GNU-Linux/main.o: main.cpp cograph.h vertice.h reader.h \
cotree.h InputHandler.h
cograph.h:
vertice.h:
reader.h:
cotree.h:
InputHandler.h:
build/Debug/GNU-Linux/reader.o: reader.cpp reader.h cograph.h vertice.h
reader.h:
cograph.h:
vertice.h:
build/Debug/GNU-Linux/vertice.o: vertice.cpp vertice.h cograph.h reader.h
vertice.h:
cograph.h:
reader.h:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: graph.cpp
* Author: marvin
*
* Created on October 25, 2021, 2:32 PM
*/
#include "cograph.h"
void graph::verticeUnion(unsigned long long* dest, unsigned long long* other, unsigned short size){
for(int i = 0; i < size/DATA_SIZE + 1; i++){
dest[i] |= other[i];
}
}
void graph::verticeInverseUnion(unsigned long long* dest, unsigned long long* other, unsigned short size){
for(int i = 0; i < size/DATA_SIZE + 1; i++){
dest[i] |= ~ other[i];
}
}
void graph::verticeInversion(unsigned long long* dest, unsigned short size){
for(int i = 0; i < size/DATA_SIZE + 1; i++){
dest[i] = ~dest[i];
}
}
void graph::verticeInverseXOR(unsigned long long * dest, unsigned long long * other, unsigned short size){
for(int i = 0; i < size/DATA_SIZE + 1; i++){
dest[i] ^= ~other[i];
}
}
vector<unsigned long long*> graph::getConnections(bool invert, unsigned long long* lookAt){
unsigned short numVerts = this->verticePointers.size(); //number of verts in the graph
unsigned long long * visited = (unsigned long long*)calloc(numVerts/DATA_SIZE,DATA_SIZE); //stores visited vertices
verticeInverseUnion(visited, lookAt, numVerts); //fill visited with stuff we do not want to visit
vector<unsigned long long*> components;
for(int i=0; i < numVerts ; i++ ){
if( ((unsigned long long)visited[i / DATA_SIZE] >> (DATA_SIZE - 1 - (i % DATA_SIZE))) % 2 == 0){ //is the vertex[i] not visited?
unsigned long long * component = (unsigned long long*)calloc(numVerts/DATA_SIZE,DATA_SIZE); //component data
verticeInverseUnion(component, lookAt,numVerts); //All Vertices not to look at will be 1 (*)
vertice* current = this->verticePointers.at(i); // take current vertice
searchAllConnected(current, component , invert,this->verticePointers); //create component
verticeInverseXOR(component, lookAt,numVerts); //reverse step (*)
verticeUnion(visited,component, numVerts); // write the component to the visited
components.push_back(component); //add the component to our components vector
}
}
return components;
}
void graph::searchAllConnected(vertice* vert, unsigned long long* binary,bool invert,vector<vertice*> verticePointers){
unsigned short numVerts= verticePointers.size(); //graph size
unsigned short numOfThisVert = vert->getNum(); //takes the index of this vertex
unsigned long long disjunion[numVerts/DATA_SIZE + 1]; //variable where we store wich vertices we have to visit from this vertex
unsigned long long * realConnected = vert->getConnections(); //take the bitrepresentation for connected vertices
unsigned long long connectedCopy[numVerts/DATA_SIZE+1];
for(int i = 0; i < numVerts/DATA_SIZE + 1;i++){ //loop over our Windows of the Bitrepr of connected vertices
connectedCopy[i] = (invert)? ~realConnected[i]: realConnected[i]; //invert if that shit has to be inverted
disjunion[i] = connectedCopy[i] ^ (binary[i] & connectedCopy[i]); //calculates the vertices we haveent visited but can visit from this vertex
binary[i] |= connectedCopy[i]; // adds the vertices that will be visited from this vertex to the visited variable
}
binary[numOfThisVert/DATA_SIZE] |= ((unsigned long long)1) << DATA_SIZE - 1 - (numOfThisVert % DATA_SIZE); //adds the vertex itseklf to the visited
if(disjunion){ //do the loop only if there are vertices to visit
for(int i = 0; i < numVerts; i++){//könnte man nachdem nur noch nullen kommen + mod DATA_SIZE - 1 ^-1 skippen
unsigned long long currentVertice = disjunion[i/DATA_SIZE] >> DATA_SIZE - 1 -(i % DATA_SIZE); //vertex(i) to index DATA_SIZE - 1
if( currentVertice % 2 == 1 && i != numOfThisVert){ //should we visit this vertex?
searchAllConnected(verticePointers.at(i), binary,invert, verticePointers);
}
}
}
}
graph::graph(){}
string graph::get_string(){
string graphAsString="Vertices:\n";
for(int v=0; v < this->verticePointers.size(); v++){
graphAsString.append("\n{");
graphAsString.append(this->verticePointers.at(v)->to_string());
graphAsString.append("}\n");
graphAsString.append(this->verticePointers.at(v)->out_to_string());
}
return graphAsString;
}
graph::graph(const graph& orig) {
}
graph::~graph() {
}
graph::graph(string path){
GraphBinary file = (*new reader(path)).getData();
this->constructFromBinary(file);
}
unsigned int graph::countBinaryOnes(unsigned long long binary){
unsigned int count;
for(count = 0; binary; count++ ){
binary &= binary - 1;
}
return count;
}
unsigned int graph::countBinaryOnes(unsigned long long* binary){
unsigned int count = 0;
unsigned int n = this->numberVertices;
for(unsigned int i=0; i < (int)(n*n)/DATA_SIZE;i++){
count += this->countBinaryOnes(binary[i]);
}
return count;
}
bool graph::getBitByNum(short num, unsigned long long byte){
return (byte >> DATA_SIZE - num - 1 ) & 1;
}
void graph::constructFromBinary(GraphBinary data){
this->numberVertices = (unsigned short)data.number;
this->numberEdges = (unsigned int) this->countBinaryOnes(data.edges);
//std::cout << "\n" << this->numberEdges << std::endl;
this->vertices = new vertice[data.number]();
this->verticePointers = *new vector<vertice*>;
for(int i=0; i < this->numberVertices ;i++ ){
this->vertices[i] = * new vertice(i);
this->vertices[i].initOut(this->numberVertices - 1);
this->vertices[i].setGraph(this);
this->verticePointers.push_back(&vertices[i]);
}
unsigned int edgecounter = 0;
for(int i = 0; i < data.number * data.number;i++){
if(this->getBitByNum((i % DATA_SIZE),data.edges[i/DATA_SIZE])){
//std::cout << data.edges[i/DATA_SIZE]+0 << std::endl;
this->vertices[i/data.number].setOneInOut(i % data.number);
}
}
}
unsigned short graph::getSize(){
return (unsigned short) this->verticePointers.size();
}
vertice * graph::getVerticeByNumber(unsigned short num){
return this->verticePointers.at(num);
}
\ No newline at end of file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: graph.h
* Author: marvin
*
* Created on October 25, 2021, 2:32 PM
*/
#ifndef GRAPH_H
#define GRAPH_H
#include <vector>
#include "vertice.h"
#include "reader.h"
#include <string.h>
#include <vector>
#define DATA_SIZE sizeof(unsigned long long)
using namespace std;
using std::string;
class vertice;
struct GraphBinary;
class graph {
public:
graph();
/**
* New Graph from File
* @param path filepath (local)
*/
graph(string path);
graph(const graph& orig);
virtual ~graph();
/**
* Returns String with the Data represented in Ascii-Visualized Data
* @return
*/
string get_string();
unsigned short getSize();
/**
* Returns vertice with corrosponding index
* @param num index
* @return vertex[num]
*/
vertice * getVerticeByNumber(unsigned short num);
/**
* Searches for all components inside the graph
* @param invert Should the graph be inverted
* @param lookAt which Verts should be looked at?
* @return vector of binary representation of components
*/
vector<unsigned long long*> getConnections(bool invert,unsigned long long* lookAt);
/**
* Inverts the given Vertice-Outputs on the Binary Data
* @param dest Given input
* @param size Graph size
*/
void verticeInversion(unsigned long long* dest, unsigned short size);
private:
/**
* Unites the Output of two vertices
* @param dest vertdata one and in which to store the value
* @param other vertdata two
* @param size size of the graph (length of the arrays)
*/
void verticeUnion(unsigned long long* dest, unsigned long long* other, unsigned short size);
/**
* Unites the Output of two vertices where the second one is inverted before
* @param dest vertdata one and in which to store the value
* @param other vertdata two (which will get inverted before the union)
* @param size size of the graph (length of the arrays)
*/
void verticeInverseUnion(unsigned long long* dest, unsigned long long* other, unsigned short size);
/**
* XOR's the Output of two vertices where the second one is inverted before
* @param dest vertdata one and in which to store the value
* @param other vertdata two (which will get inverted before the XOR)
* @param size size of the graph (length of the arrays)
*/
void verticeInverseXOR(unsigned long long * dest, unsigned long long * other, unsigned short size);
/**
* Collects the component in which the Vertice lays
* @param vert Vertice to look at
* @param binary Vertices already inside the Component/to ignore
* @param invert Should the graph be inverted?
* @param pointers Collection ov verticePointers
*/
void searchAllConnected(vertice *vert,unsigned long long* binary, bool invert, vector<vertice*> pointers);
void constructFromBinary(GraphBinary data);
/**
* Counts binary Ones on Char-Array (binary Data of Char array)
* @param binary the binary data in Char array
* @return number of ones in Chararray
*/
unsigned int countBinaryOnes(unsigned long long* binary);
/**
* Counts binaray ones on Char in O(m) with m being the number of ones
* @param binary the binary data as CHar array
* @return number of ones in Char
*/
unsigned int countBinaryOnes(unsigned long long binary);
/**
* Returns the State of the bit on the num place in the Char (num>7 --> false)
* @param num Index of the bit in the Char
* @param byte the binary data in Char
* @return the corrosponding boolean value
*/
bool getBitByNum(short num, unsigned long long byte);
/**
* Calculates the fast Gaussian sum
* @param n
* @return
*/
//Array of actual hold Vertices in the Graph
vertice* vertices;
//array of pointers pointing to actual vertices, if the graph has no actual data, but operates on other graphs (eg. it's subgraph)
vector<vertice*> verticePointers;
//at the moment senseless
unsigned int numberEdges;
//Number of Vertices saved in "vertices"
unsigned short numberVertices;
};
#endif /* GRAPH_H */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: marvin
*
* Created on October 25, 2021, 2:32 PM
*/
#include <cstdlib>
using namespace std;
#include "cograph.h"
#include "cotree.h"
#include <vector>
#include "InputHandler.h"
/*
*
*/
int main(int argc, char** argv) {
InputHandler ih = *new InputHandler(argv, argc);
return 0;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: reader.h
* Author: marvin
*
* Created on October 25, 2021, 2:33 PM
*/
#ifndef READER_H
#define READER_H
#include <string.h>
#include <fstream>
#include <sstream>
#include "cograph.h"
#include <iostream>
using namespace std;
using std::string;
#define DATA_SIZE sizeof(unsigned long long)
struct GraphBinary{
int number;
unsigned long long * edges;
};
class reader {
public:
reader(string path);
reader(const reader& orig);
virtual ~reader();
GraphBinary getData();
private:
string readFileToString(string path);
GraphBinary readBinaryFromFile();
GraphBinary readBinaryFromGraphFile();
string path;
};
#endif /* READER_H */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: vertice.h
* Author: marvin
*
* Created on October 27, 2021, 11:56 AM
*/
#ifndef VERTICE_H
#define VERTICE_H
#include <string>
#include <vector>
#include "cograph.h"
#define DATA_SIZE sizeof(unsigned long long)
class graph;
using namespace std;
using std::vector;
using std::string;
class vertice {
public:
graph* getGraph();
/**
* Sets the graph pointer
* @param g
*/
void setGraph(graph * g);
/**
* Sets the bit with index to 1
* @param index
*/
void setOneInOut(unsigned short index);
/**
* Inits Binary data for vertice-output
* @param number
*/
void initOut(unsigned short number);
vertice();
/**
* Construct vertice with given number
* @param num (label)
*/
vertice(int num);
virtual ~vertice();
/**
* Returns number of vert as string
* @return
*/
string to_string();
/**
* returns Connected-vertices as strings
* @return
*/
string out_to_string();
/**
* Return Label
* @return
*/
int getNum();
/**
* Sets numberlabel
* @param num
*/
void setNum(int num);
/**
* Returns binary Representation of Connections
* @return binary repr. as Char-array
*/
unsigned long long* getConnections();
private:
/**
* Label
*/
int num;
/**
* the parent graph
*/
graph * g;
/**
*Connections to other vertices
*/
unsigned long long * out;
};
#endif /* VERTICE_H */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment