- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey! ![Eek! :eek: :eek:](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
Let $G=(V,E)$ be a directed acyclic graph. I have to write an algorithm to assign integers to the vertices of $G$ such that if there is a directed edge from vertex $i$ to vertex $j$, then $i$ is less than $j$.Is it maybe as followed??
Could you tell me if it correct?? (Wondering)
Let $G=(V,E)$ be a directed acyclic graph. I have to write an algorithm to assign integers to the vertices of $G$ such that if there is a directed edge from vertex $i$ to vertex $j$, then $i$ is less than $j$.Is it maybe as followed??
Code:
L ← Set of integers
S ← Set of all nodes with no incoming edges
while S is non-empty do
assign the lowest number i from L to first node n from S
remove the node n from S
remove the integer i from L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return G
Could you tell me if it correct?? (Wondering)