My code does not run fully

Asked

Viewed 80 times

1

Good evening, folks. I need to implement a program that always prints a sequence of three numbers where there can be no repetition.

Ex.: t = 4.

Expected output: (0 1 2) (0 1 3) (0 2 3) (1 2 3)

The program runs for t = 4, however, for t = 10, it runs only to some extent (up to i = 6). I’ve tested every part of the code and I can’t find the error, I wonder if someone could help me?

Thank you! :)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

typedef struct{
  int v1;
  int v2;
  int v3;
}Triangulo;

int main(){
int n;
scanf("%d", &n);

Triangulo T[n];

int t;
t = (n*(n-1)*(n-2))/6;
printf("t = %d\n", t);

int c = 0;
int f = c+1;
int s = f+1;

for(int i = 0; i < t; i++){
  T[i].v1 = c;
  T[i].v2 = f;
  T[i].v3 = s;

if(s != n-1){
  s++;
}else if((s == n-1)&&(f == n-2)){
  c++;
  f = c+1;
  s = f+1;
}else if(s == n-1){
  f++;
  s = f+1;
}
}

for(int j = 0; j < t; j++){
  printf("%d %d %d\n", T[j].v1, T[j].v2, T[j].v3);
}


  return 0;
}

  • Can you put the full code? In the post part is not being defined n, T and t. Do you only want triples with different values or permutations? For example, you want it to have (0 1 2) and (2 1 0) on the way out, or just (0 1 2)?

  • Hello. With the example you gave, just (0 1 2) enough. I will update the post and put the full code :)

  • You want all permutations that are of the type (i j k) such that i < j < k, Right? I left an answer that does that, but I’m still not sure if that’s exactly what you want.

1 answer

1

You want all permutations that are of the type (i j k) such that i < j < k, correct? You won’t be able to do this using just one for. Note that when we set values for i and j, for example, we still have the values of j+1 until t-1 to assign the k. Let’s take an example:

Consider that at a certain point in the algorithm i = 1 and j = 3, and that t = 10. The possible sequences fixing these values would be:

(1 3 4)
(1 3 5)
(1 3 6)
(1 3 7)
(1 3 8)
(1 3 9)

Note that we need a for that goes from j+1 until t-1 only to iterate the possible values of k. Similarly, if we fix only the value i, we need a for to go through the possible values for j and, for each value of j, a new for for values of k. I think it’s clear that we need a for for each variable.

Follows an implementation:

#include <stdio.h>

int main(void) {

    int t;

    scanf("%d", &t);

    // Considera todos os valores de 0 até t-1
    for(int i = 0; i < t; i++){

        // Considera todos os valores de i+1 até t-1 para impedir que os valores se repitam
        for(int j = i+1; j < t; j++){

            // Considera todos os valores de j+1 até t-1 para impedir que os valores se repitam
            for(int k = j+1; k < t; k++){
                printf("(%d %d %d) ", i, j, k);
            }
        }
    }

    printf("\n");
    return 0;
}

Demonstration here.

  • Got it! My problem is that I need to find a solution in linear order, that is, I will not be able to perform these 3 loops. Would you have any suggestions as to how I could resolve this?

  • So I guess you don’t need to print out all the sequences, since the number of size subsets 3 in n elements is in the order of n^3, then you can’t print them all in linear time. Can you verify that this is exactly what you need to do? Is that a question? If yes, you could add the statement?

  • So, I have a paper to deliver on Minimum Generating Tree and I have to run in time O(m), where m is the number of edges. I want to use this snippet to generate subsets of size 3 that would be vertices of triangles within my original graph. In this excerpt I use the for in function of n, basically, then it ends up getting bigger than the number of edges and this, by specification, could not happen.

  • Got it. Is there any particular property in the input graph? No algorithm exists O(m) to the problem of MST in arbitrary graphs (at least I don’t know and I didn’t find anything about in the searches I did). There are algorithms for the MST which are linear in the graph, that is O(m + n) and it is possible to find a generating tree in O(m), but not the least. I also couldn’t see how the above algorithm helps you with the minimum generator tree. You’re following some specific algorithm?

  • In fact I was trying things without basis in any algorithm, because my searches also indicated to me what you found. I reread the specification of the work and will try to solve with minimum paths instead of MST’s. Anyway, thanks for the attention and the answers!

  • Okay. If you can put part of the specification to try to tell you if it’s going the right way...

  • So the teacher told us that the solution to the work is Camerini’s algorithm for Minimum Bottleneck Spanning Tree. Do you know anything about it? I’m struggling to interpret how it should be implemented.

  • I tried to do an implementation with Kruskal, but I can’t get away from O(m log n).

  • I guess there’s no escape from O(m lg n) using Kruskal (only if you didn’t order the edges, but then you couldn’t guarantee anything). The Camerini’s algorithm is really what you need. The biggest difficulty is to calculate the median in O(n), but you can use the algorithm Median of medians, that gives you an approximate median, and that’s enough. I made an implementation of median of medians in c, follows the link. More about the rest of the algorithm you can see here.

  • To create the forest with the subgraph formed only by the edges with less than the median value, just select these edges and apply a DFS. In the previous link have examples of implementations of DFS.

  • I read the link, but I still have doubts about how the algorithm works. I don’t understand why this part should be done: If the subgraph is Connected, V is the upper limit of the Answer, and decrease the V, repeat the step 1, 2. * If the subgraph is not Connected, Let the Connected Component to become a Node, and Increase the V, repeat the step 1, 2.

  • I don’t understand the reason for this condition. The truth is that I still can’t visualize how the algorithm should work, I understood that I have to calculate the median but I don’t understand how this will help the other parts. Anyway, thank you for the example code from the Median of medians!

  • What the algorithm does is to divide the edges into two sets: A formed by children smaller than the median and B larger ones. If it is possible to build a generating tree using only the smaller edges (from A), the algorithm repeats the process considering only the edges of A. If not possible, the algorithm takes the largest tree it can get using only edges of A and considers this tree as a vertex and repeats the process now for the new graph. Note that all edges of B are larger than those of A, so in the second case it is always safe to pick up edges of A.

  • Using the median, the idea is that the sizes of A and B be approximately m/2, so at each step the number of edges of the graph is halved.

  • Okay, but I still have a few questions. From the TP description, the graph will be complete. In that case, could I make sure the tree formed doesn’t have cycles? Another question is that I won’t need to order the edges to find the median, so how do I find a good way to find that value? And I still don’t quite understand this question of considering the tree a vertex and joining it to another edge. What edge would that be since my edges would be out of order? I will try to take some values from one of the input files and post here to try to clarify another doubt.

  • 8615 &#xA;11916 &#xA;16355 &#xA;12755 &#xA;15219 &#xA;11364 &#xA;9739 &#xA;13373 &#xA;3151 &#xA;3500 &#xA;8566 &#xA;15285 &#xA;6617 &#xA;16484 &#xA;13096 &#xA;5947 &#xA;11737 &#xA;5067 &#xA;14165 &#xA;3569 &#xA;14263 &#xA;14288 &#xA;2728 &#xA;15063 &#xA;10913 &#xA;3163 &#xA;9893 &#xA;13660 &#xA;3026 &#xA;18147 &#xA;10743 &#xA;8019 &#xA;3565 &#xA;13810 &#xA;10111 &#xA;12865 &#xA;12077 &#xA;3696 &#xA;18201 &#xA;10430 &#xA;11542 &#xA;8936 &#xA;15773 &#xA;7915 &#xA;16064

  • The answer to this entry is 8615.

  • The problem is that when I select the value at the position of the median, the value is 2728 which is my lowest value, then the side with smaller edges becomes empty.

  • Given the set A edge, you need to choose a subset T of edges that form a tree. You don’t need to pick up all the edges of A, just take a subset. That way you make sure it’s a tree (just choose edges to be a tree). Note that you will not always be able to connect all the vertices in this tree, hence you fall in the second case of the algorithm.

  • To get the median you can use the algorithm median of medians which I sent in a previous comment, it will give you an approximate median in O(n). To the entrance that put his exit is 12148.50. When partitioning the edges into smaller or equal (A) and larger (B) using the median returned by the algorithm, the set A would have 27 edges and the B would have 18. It is an approximate average, the algorithm will not always divide exactly in half, but it will get close. Here the algorithm calculating the median for the input you placed.

  • About considering the tree as a single knot. Note that at the end you need to return a generating tree (that is, a tree that connects all vertices), so if you already have a tree T with a subset of vertices, connect a new vertex u à T can be made by any connecting edge u to a vertex v in T, because the other vertices of T will also be connected to u after the addition of the edge (u, v). And all the vertices of T are already connected to each other, so you can abstract these connections and focus only on the missing ones.

  • A fix. You need to find a forest using the edges in A and not a tree. This means that it can be a set of trees (and, more formally, it simply means that the graph is acyclic, but not necessarily connected). For this, just apply the Algoritmo de Kruskal unedited. If at the end the algorithm finds only one tree, you call the recursion for the set A, if you find more than one tree, you consider each tree as a knot and call to B. Has several implementations of Kruskal on the internet, follows one.

  • So your algorithm would look something like this: pseudocode 1 and pseudocode 2.

  • I’ll try an implementation and post here!

Show 19 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.