This is a part of my first assignment for my data structures course, so I'll be happy if you just show me where I am wrong instead of posting a working code.

We're supposed to write a program that given a degree sequence, draws a graph. I wrote the data structure for the graph, it can properly connect two vertices(graph.addConnection). But I couldn't be able to find a way to build a graph from a degree sequence.

[This Wikipedia page](https://en.wikipedia.org/wiki/Degree_sequence#Degree_sequence) gives a simple algorithm:

> 
> 1. Begin with a graph with no edges.
> 2. Maintain a list of vertices whose degree requirement has not yet been met in non-increasing order of residual degree requirement.
> 3. Connect the first vertex to the next d1 vertices in this list, and then remove it from the list. Re-sort the list and repeat until all
>  degree requirements are met.
> 
> 
> 

And I implemented it in Java like this:

```java
public static void populate(Graph graph, int[] degrees) {
    class DegreeMapping {
        int vertice=0;
        int degree=0;
    }

    ArrayList<DegreeMapping> degrees_ = new ArrayList<DegreeMapping>(degrees.length);
    for(int i=0; i<degrees.length; i++) {
        degrees_.add(new DegreeMapping());
        degrees_.get(i).vertice = i;
        degrees_.get(i).degree = degrees[i];
    }

    while(! degrees_.isEmpty()) {
        // Sort by degrees
        Collections.sort(degrees_, new Comparator<DegreeMapping>() {
                                @Override
                                public int compare(DegreeMapping o1, DegreeMapping o2) {
                                    return o2.degree - o1.degree ;
                                }
                            }); 
        for(DegreeMapping i: degrees_) System.out.printf("{%d, #%d} ", i.degree, i.vertice );
        System.out.println();

        for(int i=1; i<degrees_.get(0).degree+1; i++) {
            degrees_.get(i).degree--;
            graph.addConnection(degrees_.get(0).vertice, degrees_.get(i).vertice);
            System.out.printf("#%d <-> #%d \n", degrees_.get(0).vertice, degrees_.get(i).vertice);
        }

        degrees_.remove(0); 
    }
}

```

It gives this output for the degree sequence `2 2 2 2 2 2 2 2`:

```
{2, #0} {2, #1} {2, #2} {2, #3} {2, #4} {2, #5} {2, #6} {2, #7} 
#0 <-> #1 
#0 <-> #2 
{2, #3} {2, #4} {2, #5} {2, #6} {2, #7} {1, #1} {1, #2} 
#3 <-> #4 
#3 <-> #5 
{2, #6} {2, #7} {1, #4} {1, #5} {1, #1} {1, #2} 
#6 <-> #7 
#6 <-> #4 
{1, #7} {1, #5} {1, #1} {1, #2} {0, #4} 
#7 <-> #5 
{1, #1} {1, #2} {0, #5} {0, #4} 
#1 <-> #2 
{0, #2} {0, #5} {0, #4} 
{0, #5} {0, #4} 
{0, #4} 

```

As you can see, it created two distinct groups with vertices {0, 1, 2} and {3, 4, 5, 6, 7} , there isn't any connection between them. But it supposed to create just one graph.

What am I doing wrong?