Advertisement
tiberiup

Matrice de adiacenta

Jun 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream f("Graph.in");
  7.  
  8. class UndirectedGraph
  9. {
  10. int nE,nV,m[15][15];
  11. public:
  12. void read();
  13. void showEdges();
  14. void bfs(int start);
  15. int* dfs(int start, bool enablePrinting);
  16. UndirectedGraph complement();
  17. void showDegrees();
  18. void showVerticesAscDegree();
  19. void showVerticesEvenDegree();
  20. void countEvenUnevenDegrees();
  21. bool isRegular();
  22. bool isConnected();
  23. void showConnectedComponents();
  24. bool isComplete();
  25. bool hasIsolaTedNodes();
  26. bool isChain(int v[], int n);
  27. void cyclesDFS(int currentNode, int parent, bool *flag, int v[]);
  28. bool hasCycles();
  29. bool isTree();
  30. void chainBack1(int st[], int k);
  31. void showChains();
  32. void chainBack2(int st[], int k, int sol[], int *maxLength);
  33. void showLongestChain();
  34. void bipartiteBFS(int currentNode, bool *flag, int v[]);
  35. bool isBipartite();
  36. void showRequiredConnectingEdges();
  37. };
  38.  
  39. class DirectedGraph
  40. {
  41. int nE,nV,m[15][15];
  42. public:
  43. void read();
  44. void showEdges();
  45. void showIndegreesOutdegrees();
  46. void showStrongConnectedComponents();
  47.  
  48. };
  49.  
  50. int main()
  51. {
  52. UndirectedGraph g;
  53. g.read();
  54. g.showEdges();
  55. g.showRequiredConnectingEdges();
  56. /*
  57. DirectedGraph g;
  58. g.read();
  59. g.showEdges();
  60. g.showIndegreesOutdegrees();
  61. g.showStrongConnectedComponents();
  62. */
  63. return 0;
  64. }
  65.  
  66. void UndirectedGraph::read()
  67. {
  68. int i,j,V1,V2;
  69.  
  70. f>>nV>>nE;
  71. for(i=0;i<nV;i++)
  72. for(j=0;j<nV;j++)
  73. m[i][j]=0;
  74. i=0;
  75. for(i=0;i<nE;i++)
  76. {
  77. f>>V1>>V2;
  78. if(V1<nV&&V2<nV)
  79. {
  80. m[V1][V2]=1;
  81. m[V2][V1]=1;
  82. }
  83. }
  84.  
  85. }
  86.  
  87. void UndirectedGraph::showEdges()
  88. {
  89. int i,j;
  90. cout<<endl<<nV<<' '<<nE<<endl<<"E={ ";
  91. for(i=0;i<nV;i++)
  92. {
  93. for(j=i+1;j<nV;j++)
  94. {
  95. if(m[i][j]==1)
  96. cout<<"{"<<i<<","<<j<<"}";
  97. }
  98.  
  99. }
  100. cout<<" }";
  101. /*
  102. cout<<endl;
  103. for(i=0;i<nV;i++)
  104. {
  105. for(j=0;j<nV;j++)
  106. cout<<m[i][j]<<' ';
  107. cout<<endl;
  108. }
  109. */
  110. }
  111.  
  112. void UndirectedGraph::bfs(int start)
  113. {
  114. int q[15],v[15],pos=0,nr=1,i;
  115. q[pos]=start;
  116. for(i=0;i<nV;i++)
  117. v[i]=0;
  118. v[start]=1;
  119. while(pos<nr)
  120. {
  121. for(i=0;i<nV;i++)
  122. if(m[q[pos]][i]&&!v[i])
  123. {
  124. q[nr++]=i;
  125. v[i]=1;
  126. }
  127. pos++;
  128. }
  129. cout<<endl<<"BFS("<<start<<"): ";
  130. for(i=0;i<nr;i++)
  131. cout<<q[i]<<' ';
  132. }
  133.  
  134. int* UndirectedGraph::dfs(int start,bool enablePrinting)
  135. {
  136. int *s,*v,pos=0,i;
  137. s=new int[nV];
  138. v=new int[nV];
  139. s[pos]=start;
  140. for(i=0;i<nV;i++)
  141. v[i]=0;
  142. v[start]=1;
  143. if(enablePrinting)
  144. cout<<endl<<"DFS("<<start<<"): ";
  145. while(s[0]!=-1)
  146. {
  147. if(v[s[pos]]!=2&&enablePrinting)
  148. cout<<s[pos]<<' ';
  149. v[s[pos]]=2;
  150. for(i=0;i<nV;i++)
  151. if(m[s[pos]][i]&&!v[i])
  152. {
  153. s[++pos]=i;
  154. v[i]=1;
  155. break;
  156. }
  157. if(i==nV)
  158. s[pos--]=-1;
  159. }
  160. return v;
  161. }
  162.  
  163. UndirectedGraph UndirectedGraph::complement()
  164. {
  165. UndirectedGraph g;
  166. int i,j;
  167. g.nV=nV;
  168. g.nE=nE*(nE-3)/2;
  169. for(i=0;i<g.nV;i++)
  170. for(j=0;j<g.nV;j++)
  171. if(i!=j)
  172. g.m[i][j]=1-m[i][j];
  173. return g;
  174. }
  175.  
  176. void UndirectedGraph::showDegrees()
  177. {
  178. int i,j,s;
  179. cout<<endl<<"v ";
  180. for(i=0;i<nV;i++)
  181. cout<<i<<' ';
  182. cout<<endl<<"d(v) ";
  183. for(i=0;i<nV;i++)
  184. {
  185. s=0;
  186. for(j=0;j<nV;j++)
  187. s+=m[i][j];
  188. cout<<s<<' ';
  189. }
  190. }
  191.  
  192. void UndirectedGraph::showVerticesAscDegree()
  193. {
  194. int i,j,v[15],d[15],aux,s;
  195. for(i=0;i<nV;i++)
  196. v[i]=i;
  197. for(i=0;i<nV;i++)
  198. {
  199. s=0;
  200. for(j=0;j<nV;j++)
  201. s+=m[i][j];
  202. d[i]=s;
  203. }
  204. for(i=0;i<nV-1;i++)
  205. for(j=i+1;j<nV;j++)
  206. if(d[i]>d[j])
  207. {
  208. aux=v[i];
  209. v[i]=v[j];
  210. v[j]=aux;
  211. aux=d[i];
  212. d[i]=d[j];
  213. d[j]=aux;
  214. }
  215. cout<<endl<<"Varfurile in ordinea crescatoare a gradului sunt: ";
  216. for(i=0;i<nV;i++)
  217. cout<<v[i]<<' ';
  218. cout<<endl<<"Gradul minim este "<<d[0]<<", iar gradul maxim este "<<d[nV-1]<<'.'<<endl;
  219. }
  220.  
  221. void UndirectedGraph::showVerticesEvenDegree()
  222. {
  223. int i,j,aux,s;
  224. cout<<endl<<"Varfurile cu grad par sunt: ";
  225. for(i=0;i<nV;i++)
  226. {
  227. s=0;
  228. for(j=0;j<nV;j++)
  229. s+=m[i][j];
  230. if(s%2==0)
  231. cout<<i<<' ';
  232. }
  233. }
  234.  
  235. void UndirectedGraph::countEvenUnevenDegrees()
  236. {
  237. int i,j,s,evenCount=0,unevenCount=0;
  238. for(i=0;i<nV;i++)
  239. {
  240. s=0;
  241. for(j=0;j<nV;j++)
  242. s+=m[i][j];
  243. if(s%2)
  244. unevenCount++;
  245. else
  246. evenCount++;
  247. }
  248. cout<<endl<<"Graful are "<<evenCount<<" noduri cu grad par si "<<unevenCount<<" noduri cu grad impar."<<endl;
  249. }
  250.  
  251. bool UndirectedGraph::isRegular()
  252. {
  253. int i,j,d[15],s;
  254. for(i=0;i<nV;i++)
  255. {
  256. s=0;
  257. for(j=0;j<nV;j++)
  258. s+=m[i][j];
  259. d[i]=s;
  260. }
  261. for(i=0;i<nV-1;i++)
  262. if(d[i]!=d[i+1])
  263. return false;
  264. return true;
  265. }
  266.  
  267. bool UndirectedGraph::isConnected()
  268. {
  269. int *v=this->dfs(0,false),i;
  270. for(i=0;i<nV;i++)
  271. if(v[i]==0)
  272. return false;
  273. return true;
  274. }
  275.  
  276. void UndirectedGraph::showConnectedComponents()
  277. {
  278. int n=1,*v1=this->dfs(0,false),*v2,i,j;
  279. cout<<endl<<"Componenta conexa "<<n<<": ";
  280. for(i=0;i<nV;i++)
  281. {
  282. if(v1[i]!=0)
  283. {
  284. cout<<i<<' ';
  285. }
  286. }
  287. for(j=0;j<nV;j++)
  288. {
  289. if(v1[j]==0)
  290. {
  291. v2=this->dfs(j,false);
  292. cout<<endl<<"Componenta conexa "<<++n<<": ";
  293. for(i=0;i<nV;i++)
  294. {
  295. if(v2[i]!=0)
  296. {
  297. cout<<i<<' ';
  298. v1[i]=v2[i];
  299. }
  300. }
  301. delete v2;
  302. }
  303. }
  304. cout<<endl<<"Graful este "<<100./n<<"% conex";
  305. }
  306.  
  307. bool UndirectedGraph::isComplete()
  308. {
  309. int i,j;
  310. for(i=0;i<nV;i++)
  311. for(j=0;j<nV;j++)
  312. if(i!=j&&!m[i][j])
  313. return false;
  314. return true;
  315. }
  316.  
  317. bool UndirectedGraph::hasIsolaTedNodes()
  318. {
  319. int i,j,s;
  320. for(i=0;i<nV;i++)
  321. {
  322. s=0;
  323. for(j=0;j<nV;j++)
  324. s+=m[i][j];
  325. if(!s)
  326. return true;
  327. }
  328. return false;
  329. }
  330.  
  331. bool UndirectedGraph::isChain(int v[],int n)
  332. {
  333. int i;
  334. for(i=1;i<n;i++)
  335. if(!m[v[i-1]][v[i]])
  336. return false;
  337. return true;
  338. }
  339.  
  340. void UndirectedGraph::cyclesDFS(int currentNode, int parent, bool *flag, int v[])
  341. {
  342. int i;
  343. v[currentNode]=1;
  344. for(i=0;i<nV;i++)
  345. if(m[currentNode][i])
  346. if(v[i])
  347. {
  348. if(i!=parent)
  349. {
  350. *flag=true;
  351. }
  352. }
  353. else
  354. this->cyclesDFS(i,currentNode,flag,v);
  355. }
  356.  
  357. bool UndirectedGraph::hasCycles()
  358. {
  359. bool flag=false;
  360. int i,v[nV];
  361. for(i=0;i<nV;i++)
  362. v[i]=0;
  363. this->cyclesDFS(0,-1,&flag,v);
  364. return flag;
  365. }
  366.  
  367. bool UndirectedGraph::isTree()
  368. {
  369. if(this->isConnected()&&!this->hasCycles())
  370. return true;
  371. return false;
  372. }
  373.  
  374. void UndirectedGraph::chainBack1(int st[], int k)
  375. {
  376. int i,j,ok;
  377. if(k>2)
  378. {
  379. cout<<endl;
  380. for(i=0;i<k;i++)
  381. cout<<st[i]<<' ';
  382. }
  383. if(k<nV)
  384. {
  385. for(i=0;i<nV;i++)
  386. {
  387. //cout<<i<<' ';
  388. ok=1;
  389. for(j=0;j<k;j++)
  390. if(st[j]==i)
  391. {
  392. ok=0;
  393. break;
  394. }
  395. if(k>0)
  396. if(!m[st[k-1]][i])
  397. ok=0;
  398. if(ok)
  399. {
  400. st[k]=i;
  401. this->chainBack1(st,k+1);
  402. }
  403. }
  404. }
  405. }
  406.  
  407. void UndirectedGraph::showChains()
  408. {
  409. int st[nV];
  410. this->chainBack1(st,0);
  411. }
  412.  
  413. void UndirectedGraph::chainBack2(int st[], int k, int sol[], int *maxLength)
  414. {
  415. int i,j,ok;
  416. if(k>*maxLength)
  417. {
  418. *maxLength=k;
  419. for(i=0;i<k;i++)
  420. sol[i]=st[i];
  421. }
  422. if(k<nV)
  423. {
  424. for(i=0;i<nV;i++)
  425. {
  426. ok=1;
  427. for(j=0;j<k;j++)
  428. if(st[j]==i)
  429. {
  430. ok=0;
  431. break;
  432. }
  433. if(k>0)
  434. if(!m[st[k-1]][i])
  435. ok=0;
  436. if(ok)
  437. {
  438. st[k]=i;
  439. this->chainBack2(st,k+1,sol,maxLength);
  440. }
  441. }
  442. }
  443. }
  444.  
  445. void UndirectedGraph::showLongestChain()
  446. {
  447. int st[nV], sol[nV], maxLength=0, i;
  448. this->chainBack2(st,0,sol,&maxLength);
  449. cout<<endl;
  450. if(maxLength<3)
  451. cout<<"Graful nu are niciun lant";
  452. else
  453. {
  454. cout<<"Cel mai lung lant este: ";
  455. for(i=0;i<maxLength;i++)
  456. cout<<sol[i]<<' ';
  457. }
  458. }
  459.  
  460. void UndirectedGraph::bipartiteBFS(int currentNode, bool *flag, int v[])
  461. {
  462. int i,foundColor1=0,foundColor2=0;
  463. for(i=0;i<nV;i++)
  464. if(m[currentNode][i])
  465. if(v[i]==1)
  466. foundColor1=1;
  467. else if(v[i]==2)
  468. foundColor2=1;
  469. if(!foundColor1)
  470. v[currentNode]=1;
  471. else
  472. {
  473. if(!foundColor2)
  474. v[currentNode]=2;
  475. else
  476. *flag=false;
  477. }
  478. if(*flag==false)
  479. return ;
  480. for(i=0;i<nV;i++)
  481. if(m[currentNode][i]&&!v[i])
  482. this->bipartiteBFS(i,flag,v);
  483. }
  484.  
  485. bool UndirectedGraph::isBipartite()
  486. {
  487. bool flag=true;
  488. int i,v[nV];
  489. for(i=0;i<nV;i++)
  490. v[i]=0;
  491. this->bipartiteBFS(0,&flag,v);
  492. return flag;
  493. }
  494.  
  495. void UndirectedGraph::showRequiredConnectingEdges()
  496. {
  497. int n=1,v[nV],*v1=this->dfs(0,false),*v2,i,j;
  498. v[0]=0;
  499. for(j=0;j<nV;j++)
  500. {
  501. if(v1[j]==0)
  502. {
  503. v[n++]=j;
  504. v2=this->dfs(j,false);
  505. for(i=0;i<nV;i++)
  506. if(v2[i]!=0)
  507. v1[i]=v2[i];
  508. delete v2;
  509. }
  510. }
  511. if(n==1)
  512. cout<<endl<<"Graful este deja conex";
  513. else
  514. {
  515. cout<<endl<<"Pentru ca graful sa devina conex, se pot adauga urmatoarele muchii: ";
  516. for(i=0;i<n-2;i++)
  517. cout<<'('<<v[i]<<','<<v[i+1]<<"), ";
  518. cout<<'('<<v[n-2]<<','<<v[n-1]<<')';
  519. }
  520.  
  521. }
  522.  
  523. void DirectedGraph::read()
  524. {
  525. int i,j,V1,V2;
  526.  
  527. f>>nV>>nE;
  528. for(i=0;i<nV;i++)
  529. for(j=0;j<nV;j++)
  530. m[i][j]=0;
  531. i=0;
  532. for(i=0;i<nE;i++)
  533. {
  534. f>>V1>>V2;
  535. if(V1<nV&&V2<nV)
  536. m[V1][V2]=1;
  537. }
  538.  
  539. }
  540.  
  541. void DirectedGraph::showEdges()
  542. {
  543. int i,j;
  544. cout<<endl<<nV<<' '<<nE<<endl<<"E={ ";
  545. for(i=0;i<nV;i++)
  546. {
  547. for(j=0;j<nV;j++)
  548. {
  549. if(m[i][j]==1)
  550. cout<<"{"<<i<<","<<j<<"}";
  551. }
  552.  
  553. }
  554. cout<<" }";
  555. /*
  556. cout<<endl;
  557. for(i=0;i<nV;i++)
  558. {
  559. for(j=0;j<nV;j++)
  560. cout<<m[i][j]<<' ';
  561. cout<<endl;
  562. }
  563. */
  564. }
  565.  
  566. void DirectedGraph::showIndegreesOutdegrees()
  567. {
  568. int i,j,s;
  569. cout<<endl<<"v ";
  570. for(i=0;i<nV;i++)
  571. cout<<i<<' ';
  572. cout<<endl<<"d+(v) ";
  573. for(i=0;i<nV;i++)
  574. {
  575. s=0;
  576. for(j=0;j<nV;j++)
  577. s+=m[i][j];
  578. cout<<s<<' ';
  579. }
  580. cout<<endl<<"d-(v) ";
  581. for(i=0;i<nV;i++)
  582. {
  583. s=0;
  584. for(j=0;j<nV;j++)
  585. s+=m[j][i];
  586. cout<<s<<' ';
  587. }
  588. }
  589.  
  590. void DirectedGraph::showStrongConnectedComponents()
  591. {
  592. int md[15][15],v[15],i,j,k,n=0;
  593. for(i=0;i<nV;i++)
  594. {
  595. v[i]=0;
  596. for(j=0;j<nV;j++)
  597. md[i][j]=m[i][j];
  598. }
  599. for(k=0;k<nV;k++)
  600. for(i=0;i<nV;i++)
  601. for(j=0;j<nV;j++)
  602. if(k!=i&&k!=j&&!md[i][j])
  603. md[i][j]=md[i][k]*md[k][j];
  604. cout<<endl;
  605. for(i=0;i<nV;i++)
  606. {
  607.  
  608. for(j=0;j<nV;j++)
  609. cout<<md[i][j]<<' ';
  610. cout<<endl;
  611. }
  612. cout<<endl;
  613. for(i=0;i<nV;i++)
  614. {
  615. if(!v[i])
  616. {
  617. cout<<"Componenta tare conexa "<<++n<<": "<<i<<' ';
  618. v[i]=1;
  619. for(j=0;j<nV;j++)
  620. if(i!=j&&md[i][j]*md[j][i])
  621. {
  622. v[j]=1;
  623. cout<<j<<' ';
  624. }
  625. cout<<endl;
  626. }
  627.  
  628. }
  629.  
  630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement