Graph * parseInputFile( const char * inputFile )
{
	FILE * fptr = NULL;
	int numUsers;
	float delta1;
	float delta2;
	float alpha;
	int queryId;
	int i = 0;
	Graph * graph1 = NULL;
	Graph * graph2 = NULL;
	User * userList = NULL;
	int id;
	int age;
	int gender;
	int maritalStatus;
	int race;
	int birthPlace;
	int language;
	int occupation;
	int income;

	//	Open file and check if successful
	fptr = fopen( inputFile, "r" );

	if( fptr == NULL )
	{
		printf( "ERROR!\nFile %s could not be opened!\n", inputFile );
		return 0;
	}

	//	Get lines
	fscanf( fptr, "%d,%f,%f,%d,%f", &numUsers, &delta1, &delta2, &queryId, &alpha );

	//	Allocate user List
	userList = malloc( sizeof(User) * numUsers );

	for( i = 0; i < numUsers; i++ )
	{
		//	Parse inputs
		fscanf( fptr, "%d,%d,%d,%d,%d,%d,%d,%d,%d",
				&id,
				&age,
				&gender,
				&maritalStatus,
				&race,
				&birthPlace,
				&language,
				&occupation,
				&income );

		//	Create user instance
		createUser( &userList[i], id, age, gender, maritalStatus, race,
				birthPlace, language, occupation, income );
	}

	//printUserList( userList, numUsers );

	graph1 = createGraph( userList, numUsers, delta1 );
	graph2 = createGraph( userList, numUsers, delta2 );

	/*
	int inputid;
	printf( "Enter user id for friends\n" );
	scanf( "%d", &inputid );
	while( inputid != 0 )
	{
		printFriendList( graph1, &(graph1 -> userList[ inputid - 1 ]) );
		printf( "Enter user id for friends\n" );
		scanf( "%d", &inputid );
	}

	int j;
	for( i = 0; i < numUsers; i ++ )
	{
		for( j = 0; j < numUsers; j++ )
		{
			printRelationBetween( graph1,
			&(graph1 -> userList[i]), &(graph1 -> userList[j]) );
		}
	}
	*/

	//  ~~~~~~~~~~~~~~~~~~~	QUERYS ~~~~~~~~~~~~~~~~~~~

	//	Dense Graph
	printf(  "Dense Graph : \n-------------\n" );
	printf(  "\n** Query1 **\n" );
	getMinLength( graph1, queryId );
	printf(  "\n** Query2 **\n" );
	getAllNode( graph1, queryId, alpha );
	printf(  "\n** Query3 **\n" );
	getFriends( graph1, queryId );
	printf(  "\n** Query4 **\n" );
	getFriendsOfFriends( graph1, queryId );
	printf(  "\n** Query5 **\n" );
	getAvgDegreeOfNode( graph1 );
	printf(  "\n** Query6 **\n" );
	getAvgDegreeOfSecondNode( graph1 );

	//	Sparse Graph
	printf(  "\nSparse Graph : \n-------------\n" );
	printf(  "\n** Query1 **\n" );
	getMinLength( graph2, queryId );
	printf(  "\n** Query2 **\n" );
	getAllNode( graph2, queryId, alpha );
	printf(  "\n** Query3 **\n" );
	getFriends( graph2, queryId );
	printf(  "\n** Query4 **\n" );
	getFriendsOfFriends( graph2, queryId );
	printf(  "\n** Query5 **\n" );
	getAvgDegreeOfNode( graph2 );
	printf(  "\n** Query6 **\n" );
	getAvgDegreeOfSecondNode( graph2 );

	//  ~~~~~~~~~~~~~~~~~~~	CLEAN UP  ~~~~~~~~~~~~~~~~~~~

	destroyUserList( userList );
	destroyGraph( graph1 );
	destroyGraph( graph2 );

	//	Close file
	fclose( fptr );
	return NULL;
}
Esempio n. 2
0
void Student::printInfo(){
    cout << name << " is " << "a student in the class of " << classYear << " and studies " << major <<". ";
    cout <<"He/she has " << getFriends().size() << " connections." << endl;
}
Esempio n. 3
0
GroupResultPtr AutoGroupingI::grouping(int userId)
{
	//初始化参数
	vector<int> userFriend;
	int **friendsGraph = NULL;	
	GroupResultPtr finalgroup = new GroupResult;   //最终要返回的分组结果
	
	int& friendsNumber = finalgroup->friendsNumber;
	int& friendsAccount = finalgroup->friendsAccount;
	float& friendsRate = finalgroup->friendsRate;
	int& ungroupingNumber = finalgroup->ungroupingNumber;
	Group& ungroupingMember = finalgroup->ungroupingMember;
	TotalGroup& totalgroup = finalgroup->totalGroupList;  
	finalgroup->isEmpty = 0;	

	MCE_INFO("start grouping userId : " << userId);

	//获取好友
	friendsGraph = getFriends(userId, userFriend, friendsGraph);
	friendsNumber = userFriend.size();	
	MCE_INFO("get total friendsAmount : " << friendsNumber);

	//获取全连通图
	map<int, vector<int> > group = getPureGroup(friendsNumber, friendsGraph);
	int groupAccount = group.size();	
	MCE_INFO("complete graph groupaccount : " << groupAccount);

	//下面为原始全连通图备份
	map<int, vector<int> > originalGroup(group);
	
	//向组中添加好友
	addFriendsToGroup(group, friendsGraph, friendsNumber);

	//在保证图中连通率的情况下,删除好友
	deleteFriendsFromGroup(group, originalGroup, groupAccount, friendsGraph, THRESHOLD2, 1);

	//将删除好友后的组备份
	map<int, vector<int> > beiGroup(group);

	//将相似连通图合并
	uniteSameGroup(group, groupAccount, friendsNumber, THRESHOLD3);

	//下面为重新计算图的内部连通度	
	deleteFriendsFromGroup(group, beiGroup, groupAccount, friendsGraph, THRESHOLD4, 0);
	
	//生成合并后的新组
	group = generateNewGroup(group, groupAccount);

	//统计分组好友数量
	accountFriends(group, friendsNumber, friendsAccount, friendsRate, ungroupingNumber, ungroupingMember, userFriend);

	//打印输出组员
	outputFriends(group, userId, ungroupingMember.friendsId, userFriend, ungroupingNumber, friendsAccount, friendsRate, totalgroup, friendsGraph);	

	totalgroup.push_back(ungroupingMember);       //根据产品需要,将为分组人员放在已分组人员后面

	MCE_INFO("resultgroup size : " << totalgroup.size());

    for(int i=0; i<friendsNumber; ++i)               //完成最后的内存处理
    {
        delete[] friendsGraph[i];
    }

    delete[] friendsGraph;
    friendsGraph = NULL;

	return finalgroup;
}