0

I have a List<Node> nodes. And class Node can be defined as follows:

 public class Node
{
    public int Density { get; set; }
    public List<Edge> adjnodes { get; set; }


    public Node()
    {
        adjnodes = new List<Edge>();

    }

    public static int GetDensity(Node n)
    {
        return n.Density;
    }

}

Now, each node in nodes have a density value associated with it which is calculated using following random function:

private static Random rnd = new Random();  

public static int generateRandom(int min, int max)
        {
            int random = rnd.Next(min, max);
            return random;
        }

And I use generateRandom() as follows in the static void main :

for (int i = 0; i < n; i++)
        {
            int densityRandom = YelloWPages.generateRandom(1, 10);
            nodes[i].Density = densityRandom;
            Console.WriteLine("Node is " + nodes[i] + "and density " + nodes[i].Density + "Interest.Array[0] " + nodes[i].P1.InterestArray[0]);
        }

My questions are as follows:

  1. Suppose there is a List<List<Nodes>> listofnodes. Each List in listofnodes (For example say listofnodes[0] -> which is a first element and a list). has a maximum capacity as defined by user. In our example let it be 10.

I want a forloop to check density of each node in nodes. If it is greater than 5, it should go in one of the lists of listofnodes. Now, suppose there are 100 nodes and 55 of them have density greater than 5. First 10 should go in some listofnodes[i] list while next 10 in listofnodes[i+1]... till all 55 are in one of the lists of listofnodes. But only condition is size of listofnodes[i] or listofnodes[i+1] or whichever should not be more than 10. This is something I tried but it clearly does not work out:

public static void DivideandInsert(List<Node> list)
    {
        List<List<Node>> smallList = new List<List<Node>>();
        for (int i = 0; i < list.Count; i++)
        {
            for (int k = 0; k < smallList.Count; k++)
            {
                if (list[i].Density > 5 && smallList[k].Count != 10)
                {
                    smallList[k].Add(list[i]);
                }
                else smallList[k + 1].Add(list[i]);

            }
        }
    }

If at all it would have worked. But its giving me size of smallList as 0 after the operation is performed.

  1. If at all this logic is correct, where I am going wrong?

Help to resolve this issue will be highly appreciated.

LearningAsIGo
  • 1,240
  • 2
  • 11
  • 23

3 Answers3

1

I think this how you should do it instead

public static void DivideandInsert(List<Node> list)
   {
        List<List<Node>> smallList = new List<List<Node>>();
         //an extra list
        List<Node> filtered_list = new List<Node>(); 

     //This part will get you all the 55 nodes with density > 5
     foreach(Node n in list) 
     {
       if(n.Density > 5)
       {
           filtered_list.Add(n);
       }
    } 
  int count  = filtered_list.Count;
  int sublist_count  = (count % 10 == 0) ? count / 10 : count / 10 + 1;

       for(int i=0;i<sublist_count;i++)
        {
                List<Node> sublist = new List<Node>();

                for (int k = 0; k < 10; k++)
                {
                    sublist.Add(filtered_list[k]);
                }
            smallList.Add(sublist);
        }   
  }
Rahul
  • 76,197
  • 13
  • 71
  • 125
1
List<List<Node>> smallList = 
    nodes.Where(n => n.Density > 5)
         .Select((x, i) => new { Index = i, Value = x })
         .GroupBy(x => x.Index / 10)
         .Select(x => x.Select(v => v.Value).ToList())
         .ToList();

Get the idea from here.

Community
  • 1
  • 1
Pengyang
  • 2,391
  • 3
  • 16
  • 9
0

Your smallList is empty when you try to access it.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Ya. I guessed it. And I guess that is the reason why that error. But the how that can be resolved? – LearningAsIGo Apr 30 '14 at 02:28
  • Actually I don't see how the inner loop is ever entered since smallList.Count is zero. What line is the exception thrown on? – Steve Wellens Apr 30 '14 at 02:31
  • I dont know why but exception is not thrown now. however, nothing is getting added in smallList. I checked smallList.count and its turning out to be 0. – LearningAsIGo Apr 30 '14 at 02:38