FubarDevelopment / QuickGraph

Fork of https://quickgraph.codeplex.com/
Microsoft Public License
9 stars 2 forks source link

CP-22746: EdgeListGraph - GetVertexCounts() => KeyNotFoundException #134

Open fubar-coder opened 6 years ago

fubar-coder commented 6 years ago

From unknown CodePlex user on Sunday, 13 May 2012 08:24:35

Both properties "VertexCount" and "Vertices" rely on the "GetVertexCounts()" method. The latter always fails with a KeyNotFoundException, since the vertices dictionary is initially empty.  

private Dictionary<TVertex, int> GetVertexCounts( )
{
            var vertices = new Dictionary<TVertex, int>( this.EdgeCount * 2 );
            foreach ( var e in this.Edges )
            {
                vertices[e.Source]++;
                vertices[e.Target]++;
            }
            return vertices;
}

so a solution would be e.g.:

            foreach ( var e in this.Edges )
            {
                if ( !vertices.ContainsKey( e.Source ) )
                    vertices[e.Source] = 1;
                else
                    vertices[e.Source]++;
                if ( !vertices.ContainsKey( e.Target ) )
                    vertices[e.Target] = 1;
                else
                    vertices[e.Target]++;
            }