Finding isolated nodes in graph :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream.h>
const n=10;
struct link
{ char key; link*next;}*g[n];

void init(link*g[n])
{ for(int i=0; i<n; i++)
g[i]=NULL; }


int search_node(link*g[n],char c)
{int flag=0;
for(int i=0; i<n; i++)
if(g[i])
if(g[i]->key==c)flag=1;
return flag;
}

int search_arc(link*g[n], char c1, char c2)
{ int flag=0;
if(search_node(g,c1)&&search_node(g,c2))
{ int i=0;
while(g[i]->key!=c1)i++;
link*p=g[i];
while (p->key!=c2&&p->next!=NULL)
p=p->next;
if (p->key==c2) flag=1;
}
return flag;
}

void del_node(link*g[n], char c)
{
	if(search_node(g, c))
	{
		int i=0;
		while(g[i]->key!=c)
			i++;
		link*p, *q;
		while(g[i]!=NULL)
		{ p=g[i];
		g[i]=p->next;
		delete p;
		}
		for(i=0; i<n; i++)
			if(g[i])
			{
				p=g[i];
				while((p->key!=c)&&(p->next!=NULL))
				{
					q=p;
					p=p->next;
				}
				if(p->key==c)
				{
					q->next=p->next;
					delete p;
				}
			}
	}
	else 
		cout<<"The node is not in the graph!";
}


void add_node(link *g[n],char c)
{ if(search_node(g,c))
cout<<"\n Existing node!\n";
else{int j=0;
while(g[j]&&(j<n)) j++;
if(g[j]==NULL){
	g[j]=new link;
	g[j]->key=c;
	g[j]->next=NULL;}
else cout<<"\nOverflow!\n";
}
}

void add_arc(link*g[n], char c1, char c2)
{int i=0;
link*p;
if (search_arc(g, c1, c2))
cout<<"Existing arc!";
else
{
	if (!(search_node(g, c1)))
		add_node(g, c1);
	if (!(search_node(g, c2)))
		add_node(g, c2);
	while (g[i]->key!=c1)
		i++;
	p=new link;
	p->key=c2;
	p->next=g[i]->next;
	g[i]->next=p;
}
}

void print()
{link *p;
for(int i=0;i<n;i++)
{
	p=g[i];
	cout<<'\n';
	while(p!=NULL)
	{cout<<p->key<<"->";
	if(p->next)
	cout<<"->";
	p=p->next;
	}
}
}

void izoliran(link*g[n]);

void main()
{ char c, k;
int m;
do
{ cout<<"\n Menu\n";
cout<<"1-Initialisation\n";
cout<<"2-Insert node\n";
cout<<"3-Insert arc\n";
cout<<"4-Print\n";
cout<<"5-Find isolated node\n";
cout<<"6-Del node\n";
cout<<"10-Exit";
cout<<"\n Your choice is: ";
cin>>m;
switch(m){
case(1):{init(g);break;}
case(2):{cout<<"Input c ";cin>>c;add_node(g,c);break;}
case(3):{cout<<"Input first node: ";cin>>c;cout<<"Input second node: ";cin>>k;add_arc(g,c,k);break;}
case(4):{print();break;}
case(5):{izoliran(g); break;}
case(6):{cout<<"delete node: ";cin>>c;del_node(g,c);break;}}
}while(m!=10);
}

/*void izoliran(link*g[n])
{
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(g[i]->key!=g[j]->key)
			if(search_arc(g, g[i]->key, g[j]->key))
			{
				cout<<"The node "<<g[i]->key<<" is not isolated!";
				break;
			}
		}
	}
	cout<<"The node is isolated!";
}*/


What's not right with the function that finds isolated verticles?
How to write a correct function searching for isolated nodes in the graph.Please help!
It would be very nice to post only problem code and point to it. Then explain what is problem and what do you want. It's very hard to debug those large extract of source code in mind.
Last edited on
The problem code is in the comment. The rest of the code is correct.The problem is that i cannot write a function that prints a message if there is an isolated node in the graph or not.Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void izoliran(link*g[n])
{
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(g[i]->key!=g[j]->key)
			if(search_arc(g, g[i]->key, g[j]->key))
			{
				cout<<"The node "<<g[i]->key<<" is not isolated!";
				return;//break means leave the loop, but you have two loops
			}
		}
	}
	cout<<"The node is isolated!";
}


But if you want to show all not isolated nodes you need to enter bool flag, for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void izoliran(link*g[n])
{
         bool flag = true;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(g[i]->key!=g[j]->key)
			if(search_arc(g, g[i]->key, g[j]->key))
			{
				cout<<"The node "<<g[i]->key<<" is not isolated!";
				flag = false;
			}
		}
	}
        if (flag)
        {
	    cout<<"The node is isolated!";
        }
}
Last edited on
Thank you! But I think there is a mistake in the second loop because it prints a message only for one node that is not isolated not for all of them. I cannot find the mistake but i am sure that it's in this function and like i said in the second loop :)
Did you try my second variant? It should be help
Yeah. It works only for the first node in the graph. There is a problem in the function , it checks only the first node and i can't see why. I tried both of your variants but the problem is one and the same. Maybe in the second variant there must be return , but after i added it to the function it didn't work too :|. If i add three nodes a,b,c and connect a with b using arc it must check all the three nodes and prints if every one of them is isolated or not.. I don't know how to do this..
Topic archived. No new replies allowed.