/*
	QUERY 4:	Function to get number of friends and print
				all friends
*/
void getFriendsOfFriends( Graph * graph,  int queryId )
{
	int i, j;
	int numFriends = 0;
	int numFriends2 = 0;
	int * idArray;
	int * idArray2;
	idArray = malloc( sizeof( int ) * graph -> numUsers );
	idArray2 = malloc( sizeof( int ) * graph -> numUsers );
	for( i = 0; i < graph -> numUsers; i++ )
	{
		if( graph -> relationMatrix[ queryId-1 ][i].isFriend == 1 )
		{
			idArray[ numFriends ] = i + 1;
			idArray2[ numFriends2 ] = i + 1;
			numFriends++;
			numFriends2++;
		}
	}

	int index;
	for( i = 0; i < numFriends; i++ )	//	Iterate through 1st Friends
	{
		for( j = 0; j < graph -> numUsers; j++ )	//	Iterate through all users
		{
			if( graph -> relationMatrix[ idArray[i] - 1 ][ j ].isFriend == 1 && j != queryId - 1 )
			{
				index = isPresent( idArray2, numFriends2, j + 1 );
				if( index < 0 )
				{
					insertInArray( idArray2, numFriends2, j + 1 );
					numFriends2++;
				}
			}
		}
	}

	printf(  "Num friends two hops away : %d\n", numFriends2 );
	j = numFriends2;

	printf( "Ids :\t");

	for( i = 0; i < numFriends2; i++ )
	{
		printf(  "%d", idArray2[i] );
		j--;
		if( j )
		{
			printf(  ", " );
		}
		else
		{
			printf(  "\n" );
		}
	}

	free( idArray );
	free( idArray2 );
}
/*
	QUERY 6:	Function to get average degree of second-level Friends
				in graph
*/
void getAvgDegreeOfSecondNode( Graph * graph )
{

	int k;
	int i, j;
	int numFriends = 0;
	int numFriends2 = 0;
	int * idArray;
	int * idArray2;
	int numSecondLevelFriends = 0;
	float avg;

	for( k = 0; k < graph -> numUsers; k++ )
	{
		numFriends = 0;
		numFriends2 = 0;
		idArray = malloc( sizeof( int ) * graph -> numUsers );
		idArray2 = malloc( sizeof( int ) * graph -> numUsers );
		for( i = 0; i < graph -> numUsers; i++ )
		{
			if( graph -> relationMatrix[ k ][i].isFriend == 1 )
			{
				idArray[ numFriends ] = i + 1;
				idArray2[ numFriends2 ] = i + 1;
				numFriends++;
			}
		}

		int index;
		for( i = 0; i < numFriends; i++ )	//	Iterate through 1st Friends
		{
			for( j = 0; j < graph -> numUsers; j++ )	//	Iterate through all users
			{
				if( graph -> relationMatrix[ idArray[i] - 1 ][ j ].isFriend == 1 && j != k )
				{
					index = isPresent( idArray2, numFriends2, j + 1 );
					if( index < 0 )
					{
						insertInArray( idArray2, numFriends2, j + 1 );
						numFriends2++;
					}
				}
			}
		}
		numSecondLevelFriends += numFriends2;

		free( idArray );
		free( idArray2 );
	}

	avg = (float) numSecondLevelFriends / graph -> numUsers;
	avg *= 100;
	avg = trunc( avg );
	avg = avg / 100;
	printf(  "Average Degree of Nodes : %0.2f\n", avg );
}
Beispiel #3
0
static StringArray* createArrayFromFile(const char* fileName)
{
  // Open file
  FILE* file = fopen(fileName, "r");
  if (!file)
  {
    fprintf(stderr, "Error while loading file '%s': %s\n",
            fileName, strerror(errno));
    return NULL;
  }

  // Create the Array
  StringArray* result = createEmptyArray();
  if (!result)
  {
    fprintf(stderr, "Memory allocation error while reading file %s\n", fileName);
    return NULL;
  }


  // Read file line by line
  char* line = readLine(file);
  while (line != NULL)
  {
    if (!insertInArray(result, line))
    {
      fprintf(stderr, "Memory allocation error while reading file %s\n", fileName);
      freeArray(result, true);
      return NULL;
    }

    line = readLine(file);
  }

  fclose(file);
  return result;
}
Beispiel #4
0
int add(ArrayList *list, void *data){
	if(!list) return 0;
	return insertInArray(list, list->length, data);
}