Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rbench
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stefan Braß
rbench
Commits
5093c739
Commit
5093c739
authored
5 years ago
by
Stefan Braß
Browse files
Options
Downloads
Patches
Plain Diff
New graph data structures in graph.cpp
parent
ec7fcf74
No related branches found
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
graph/alt_graph.cpp
+164
-0
164 additions, 0 deletions
graph/alt_graph.cpp
graph/graph.cpp
+487
-406
487 additions, 406 deletions
graph/graph.cpp
with
651 additions
and
406 deletions
graph/alt_graph.cpp
0 → 100644
+
164
−
0
View file @
5093c739
// Old implementation, maybe superior for some graphs.
//=============================================================================
// Class for Graph Nodes (one line of the adjacency matrix):
//=============================================================================
class
Node
{
public:
// Constructor:
Node
(
int
graph_num_nodes
)
{
num_nodes_
=
graph_num_nodes
;
int
bytes
=
(
num_nodes_
+
BYTE_SIZE
-
1
)
/
BYTE_SIZE
;
connections_
=
new
char
[
bytes
];
char
*
p
=
connections_
;
for
(
int
i
=
bytes
;
i
>
0
;
i
--
)
*
p
++
=
'\0'
;
}
// Accessor Functions:
inline
int
num_nodes
()
const
{
return
num_nodes_
;
}
// Store edge in adjacency matrix:
void
store_connection
(
int
to
)
{
// Check nodes:
if
(
to
<=
0
||
to
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid to node: "
<<
to
<<
"
\n
"
;
exit
(
3
);
}
// Determine position in adjacency matrix:
long
index
=
to
-
1
;
int
bit
=
index
%
8
;
index
=
index
/
8
;
// Set bit:
connections_
[
index
]
|=
(
0x1
)
<<
bit
;
}
// Check whether edge exists:
bool
connection_exists
(
int
to
)
const
{
// Check node:
if
(
to
<=
0
||
to
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid to node: "
<<
to
<<
"
\n
"
;
exit
(
4
);
}
// Determine position in adjacency matrix:
long
index
=
to
-
1
;
int
bit
=
index
%
8
;
index
=
index
/
8
;
// Check bit:
return
(
connections_
[
index
]
&
((
0x1
)
<<
bit
))
!=
0
;
}
// Copy other node into this node:
void
copy
(
Node
*
node
)
{
if
(
node
->
num_nodes_
!=
num_nodes_
)
{
std
::
cerr
<<
"Can copy only node of same size!
\n
"
;
exit
(
5
);
}
int
bytes
=
(
num_nodes_
+
BYTE_SIZE
-
1
)
/
BYTE_SIZE
;
char
*
to
=
connections_
;
char
*
from
=
node
->
connections_
;
while
(
bytes
--
>
0
)
*
to
++
=
*
from
++
;
}
// Attributes:
private
:
int
num_nodes_
;
char
*
connections_
;
};
//-----------------------------------------------------------------------------
// Pointer Type for Node Data:
//-----------------------------------------------------------------------------
typedef
Node
*
node_t
;
#define NODE_NULL (static_cast<node_t>(0))
//=============================================================================
// Class for Graphs (contains adjacency matrix):
//=============================================================================
class
Graph
{
public:
// Constructor:
Graph
(
int
graph_num_nodes
)
{
num_nodes_
=
graph_num_nodes
;
nodes_
=
new
node_t
[
num_nodes_
];
for
(
int
i
=
0
;
i
<
graph_num_nodes
;
i
++
)
nodes_
[
i
]
=
new
Node
(
graph_num_nodes
);
}
// Accessor Functions:
inline
int
num_nodes
()
const
{
return
num_nodes_
;
}
inline
node_t
node
(
int
i
)
const
{
return
nodes_
[
i
-
1
];
}
// Store edge in adjacency matrix:
void
store_edge
(
int
from
,
int
to
)
{
// Check nodes:
if
(
from
<=
0
||
from
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid from node: "
<<
from
<<
"
\n
"
;
exit
(
6
);
}
if
(
to
<=
0
||
to
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid to node: "
<<
to
<<
"
\n
"
;
exit
(
7
);
}
// Store edge in adjacency matrix:
long
index
=
from
-
1
;
nodes_
[
index
]
->
store_connection
(
to
);
}
// Check whether edge exists:
bool
edge_exists
(
int
from
,
int
to
)
const
{
// Check nodes:
if
(
from
<=
0
||
from
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid from node: "
<<
from
<<
"
\n
"
;
exit
(
8
);
}
if
(
to
<=
0
||
to
>
num_nodes_
)
{
std
::
cerr
<<
"Invalid to node: "
<<
to
<<
"
\n
"
;
exit
(
9
);
}
// Look up edge in adjacency matrix:
long
index
=
from
-
1
;
return
nodes_
[
index
]
->
connection_exists
(
to
);
}
// Copy a graph (of the same size) into this graph:
void
copy
(
Graph
*
g
)
{
if
(
g
->
num_nodes_
!=
num_nodes_
)
{
std
::
cerr
<<
"Can copy only graph of same size!
\n
"
;
exit
(
10
);
}
for
(
int
i
=
0
;
i
<
num_nodes_
;
i
++
)
nodes_
[
i
]
->
copy
(
g
->
nodes_
[
i
]);
}
// Attributes:
private
:
int
num_nodes_
;
node_t
*
nodes_
;
};
//-----------------------------------------------------------------------------
// Pointer Type for Graph Data:
//-----------------------------------------------------------------------------
typedef
Graph
*
graph_t
;
#define GRAPH_NULL (static_cast<graph_t>(0))
This diff is collapsed.
Click to expand it.
graph/graph.cpp
+
487
−
406
View file @
5093c739
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment