Assign integers to the vertices of G

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Integers
In summary, the algorithm involves setting a set of integers and a set of nodes with no incoming edges, and then assigning numbers to these nodes and removing them from the set until all nodes have been assigned.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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)
 
Technology news on Phys.org
  • #2
Yes, your algorithm is correct. It will 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$.
 

Related to Assign integers to the vertices of G

1. How do you assign integers to the vertices of a graph (G)?

There are a few different methods for assigning integers to the vertices of a graph, but the most common is to simply label each vertex with a unique number starting from 1. Another method is to use the coordinates of the vertices as their assigned integers.

2. Why is it important to assign integers to the vertices of a graph?

Assigning integers to the vertices of a graph is important because it allows for easier manipulation and analysis of the graph. It also helps to identify and differentiate between the different vertices in the graph.

3. What are the rules for assigning integers to the vertices of a graph?

There are no strict rules for assigning integers to the vertices of a graph, but it is important to ensure that each vertex is assigned a unique number and that the numbers are assigned in a consistent manner.

4. Can you assign negative integers to the vertices of a graph?

Yes, it is possible to assign negative integers to the vertices of a graph. This can be useful in certain situations, such as when dealing with directed graphs or weighted graphs.

5. Is there a specific order in which the vertices should be assigned integers?

There is no specific order in which the vertices should be assigned integers, as long as each vertex is assigned a unique number. However, some algorithms and methods may require a specific order for the vertices to be assigned integers in order to work properly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • General Math
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • General Math
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
Back
Top