Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cnr
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hamida Seba
Cnr
Commits
3aae5642
Commit
3aae5642
authored
3 years ago
by
Ikenna Oluigbo
Browse files
Options
Downloads
Patches
Plain Diff
Build Graph from input data
parent
fd580a15
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
build_graph.py
+76
-0
76 additions, 0 deletions
build_graph.py
with
76 additions
and
0 deletions
build_graph.py
0 → 100644
+
76
−
0
View file @
3aae5642
import
networkx
as
nx
import
matplotlib.pyplot
as
plt
def
parse_graph
():
with
open
(
'
filename.txt
'
,
'
r
'
)
as
file
:
#Graph edges name/path
f
=
file
.
readlines
()
edges
=
[]
a
=
[
int
(
n
.
split
(
'
'
)[
0
])
for
n
in
f
]
b
=
[
int
(
n
.
split
(
'
'
)[
1
])
for
n
in
f
]
nod
=
list
(
set
(
a
+
b
))
for
i
in
range
(
len
(
b
)):
edges
.
append
((
a
[
i
],
b
[
i
]))
return
nod
,
edges
def
node_labels
():
nod
,
_
=
parse_graph
()
d
=
{}
with
open
(
'
filename.txt
'
,
'
r
'
)
as
lab
:
#Graph labels name/path
l
=
lab
.
readlines
()
#Returns a list string
if
len
(
l
[
0
].
split
(
'
'
))
==
2
:
x
=
[
int
(
w
.
split
(
'
'
)[
0
])
for
w
in
l
]
y
=
[
int
(
w
.
split
(
'
'
)[
-
1
].
rstrip
(
'
\n
'
))
for
w
in
l
]
for
j
in
range
(
len
(
x
)):
d
[
x
[
j
]]
=
y
[
j
]
#For label data with node
elif
len
(
l
[
0
].
split
(
'
'
))
==
1
:
for
i
,
k
in
enumerate
(
l
,
1
):
#start counting from 1
d
[
i
]
=
int
(
k
.
rstrip
(
'
\n
'
))
#For label data without node
else
:
print
(
'
Edges more than 2 nodes
'
)
labels_dict
=
{}
for
node
in
nod
:
labels_dict
[
node
]
=
d
[
node
]
return
labels_dict
labels_dict
=
node_labels
()
only_labels
=
list
(
set
(
labels_dict
.
values
()))
def
extract_subgraph
(
G
,
source
,
destination
):
if
nx
.
has_path
(
G
,
source
,
destination
):
nodes
=
nx
.
shortest_path
(
G
,
source
,
destination
)
pattern_graph
=
G
.
subgraph
(
nodes
)
nx
.
draw
(
pattern_graph
,
with_labels
=
True
)
plt
.
show
()
else
:
print
(
'
No Path between {} and {}
'
.
format
(
source
,
destination
))
def
node_neighbors
(
G
):
neighbors
=
{}
for
node
in
G
.
nodes
:
neighbors
[
node
]
=
list
(
G
.
neighbors
(
node
))
return
neighbors
def
node_neighbor_labels
(
G
):
neighbor_labels
=
{}
for
node
in
G
.
nodes
:
nn
=
list
(
G
.
neighbors
(
node
))
#nn = Node Neighbor
nnlabel
=
[
labels_dict
[
n
]
for
n
in
nn
]
#Superimpose labels on node id
neighbor_labels
[
node
]
=
nnlabel
return
neighbor_labels
def
build
():
nod
,
edges
=
parse_graph
()
G
=
nx
.
Graph
()
G
.
add_nodes_from
(
nod
)
G
.
add_edges_from
(
edges
)
for
e
in
G
.
edges
:
G
.
edges
[
e
][
'
weight
'
]
=
1
return
G
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