Visualisierung2
AdjazenzMatrix.cpp
Go to the documentation of this file.
1 #include "AdjazenzMatrix.h"
2 
3 
5 {
6  char line[256];
7  VertexCount = 0;
8 
9  std::ifstream infile (path, std::ios::in);
10 
11  if (!infile)
12  {
13  std::cout << "File kann nicht gelesen werden" << std::endl;
14  return;
15  }
16 
17  //-----------get Vertexcout for initialisation---------------
18  while (infile.getline (line, 256))
19  {
20  int vertex1 = -1, vertex2 = -1;
21 
22  std::stringstream tmp1;
23  char* token = strtok(line, " ");
24  tmp1 << token;
25  if((tmp1 >> vertex1).fail())
26  {
27  std::cout << "Fehlerhaftes File" << std::endl;
28  return;
29  }
30  if (VertexCount < vertex1)
31  {
32  VertexCount = vertex1;
33  }
34 
35  std::stringstream tmp2;
36  token = strtok(NULL, " ");
37  tmp2 << token;
38  if((tmp2 >> vertex2).fail())
39  {
40  std::cout << "Fehlerhaftes File" << std::endl;
41  return;
42  }
43  if (VertexCount < vertex2)
44  {
45  VertexCount = vertex2;
46  }
47  std::cout<<vertex1<<";"<<vertex2<<std::endl;
48  // mach was mit der zeile
49  //cout << line << endl;
50  }
51  std::cout<<VertexCount<<std::endl;
52  //------------------------------------------------
53 
54  //----------initialize matrix--------------------
55  matrix = new double*[VertexCount];
56  for(int i = 0; i < VertexCount; ++i)
57  {
58  matrix[i] = new double[VertexCount];
59  for(int j = 0; j< VertexCount; ++j)
60  {
61  matrix[i][j] = 0.0;
62  }
63  }
64  //----------------------------------------------
65 
66  //----------create the matrix-------------------
67  infile.clear();
68  infile.seekg(0, std::ios::beg);
69  while (infile.getline (line, 256))
70  {
71  int i = -1, j = -1;
72 
73  std::stringstream tmp1;
74  char* token = strtok(line, " ");
75  tmp1 << token;
76  tmp1 >> i;
77 
78  std::stringstream tmp2;
79  token = strtok(NULL, " ");
80  tmp2 << token;
81  tmp2 >> j;
82 
83  matrix[i-1][j-1] = 1.0;
84  matrix[j-1][i-1] = 1.0;
85  // mach was mit der zeile
86  //cout << line << endl;
87  }
88  //-------------------------------------------------
89  infile.close();
90  for(int i = 0; i < VertexCount; ++i)
91  {
92  for(int j = 0; j< VertexCount; ++j)
93  {
94  std::cout<<matrix[i][j];
95  }
96  std::cout<<std::endl;
97  }
98 }
99 
100 
102 {
103  for(int i = 0; i < VertexCount; ++i)
104  {
105  delete[] matrix[i]; matrix[i] = nullptr;
106  }
107  delete[] matrix, matrix = nullptr;
108 }
int VertexCount
Wert des Maximalen Knotenpunktes im Graphen und damit auch die Anzahl der Vertizes.
double ** matrix
Enthaellt die erzeugte Adjazenzmatrix. 1..Kante vorhanden, 0..keine Kante vorhanden. Typ double: wegen der Moeglichkeit des normalisierens der Matrix.
AdjazenzMatrix(char *path)