Exp
6
Friend’s
network
Graph
Class friends
{
public:
char * name;
int comments;
int date;
int month;
int year;
};
Note : data members need to be private for pure C++
implementation . To access private members , we need member functions. Here all
data members are declared public for simplicity
Class vertex
{
public :
friends f;
vertex * next;
}
Class Graph
{
vertex *g[10];
public :
void createGraph();
void calfriends();
void maxcomments();
void samemonthbirth();
int search(char* name);
//returns index
}
Graph:: createGraph()
{
cout<<“how many
persons ?”
cin>>n;
for(i=0;i<n;i++)……………………………………………………………….//
enter info of all friends
{
vertex * temp=new
vertex;
cout<<“ enter
data”;
cin>>temp->f.name>>temp->f.comments>>temp->f.date>>………
g[i]=temp;
}
for(i=0;i<nf;i++)…………………………………………………………….//create link among friends
{
vertex * temp=new
vertex;
cin>>temp->f.name;
cout<<“ enter
how many friends of” >>
g[i]->f.name ;
Cin>>nf;
for(j=0;j<n;j++)
{
if(strcmp(g[i]->f.name, temp->f.name)
{
temp->f.comments=g[i]->f.comments;
temp->f.date=g[i]->f.date;………………………//same for all fields month,year etc
}
if
(g[i]->next==NULL)
g[i]->next=temp;
else
{
p=
g[i];………………………………………//traverse SLL of each node &
insert new link
while(p->next !=NULL)
p=p->next;
}
}
}
}
Void calfriends()
{
Int count[10]; ……………………..//initialize all slots to
zero
For(i=0;i<n;i++)
{
P=g[i];
While(p->next!=NULL)
{
p=p->next;
count[i]++;
}
}
Display contents of count
which is no. of friends of all
Find max & min on count
array & display names
g[i]->f.name
}
Send v=0 , initialize visited array &
maxcmnts to 0 in main & call maxcomments function
Void maxcomments(int v, int visited[], int maxcmts[])…………………………………….//using
recursive DFS
|
DFS-recursive(G,
s):
mark s as visited
for all neighbours w of s in Graph G:
if w is not visited:
DFS-recursive(G, w)
|
vertex *w;
visited[v]=1;
int p;
maxcmts[v]=g[v]->f.no_ofcomments;
for(w=g[v]->next; w; w=w->next)
{
p=search(w->f.name); ……//search returns index of w
if(visited[p]==0)
maxcomments(p,
visited, maxcmts);…………//dfs
}
}
//find max & min comments from array
maxcmnts in main & display names
|
BFS (G,S) //Where G is the
graph and s is the source node
Let Q be the queue //Inserting s
in queue until all its neighbour vertices
Q.enqueue(s)
// are marked.
Mark s as visited
While (Q is not empty)//Removing that vertex from queue,whose
neighbour
V=Q.dequeue() // will be visited now
//processing
all the neighbours of v
For all neighbors w of V in
Graph G
If w is not visited
|
void samemonthbirth()………………………………//using BFS
{
Queue q;
// obj of Class Queue
Vertex *temp,*temp2;
int visited[10];
//initialize all to 0 using for
cout<<”enter birth
month”;
cin>>m;
q.enqueue(g[0]);
//enqueue(vertex *) method
visited[0]=1;
while(q.is_empty()!=1)
{
temp=q.dequeue();
if(temp->f.month==m)
cout<<temp->f.name; //if none found, test case
can be written
p=search(temp->f.name);
temp2=g[p]; //scan SLL at
g[p] using temp2
while(temp2->next!=NULL)
{
temp2=temp2->next;
p=search(temp2->f.name());
if(visited[p]==0)
{
q.enqueue(temp2);
visited[p]=1;
}
}
}
}
No comments:
Post a Comment