Advertisement
Guest User

iza

a guest
Apr 26th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.48 KB | None | 0 0
  1.     //kolory kolorowania grafu:
  2.     //biały - 0 ; szaro - 1; czarno - 2
  3.     //0 - drzewowa, 1- wsteczna, 2- poprzeczna / wyprzedzajaca
  4. #include <utility>
  5. #include <iostream>
  6. #include <list>
  7. #include <vector>
  8.  
  9. #define data int
  10.  
  11.     using namespace std;
  12.         vector<int> * visited;
  13.         vector<int> * timeT;
  14.         int time = 0; //  ile razy
  15.    
  16.     using namespace std;
  17.                                
  18.                 struct node{
  19.                         data d;
  20.                         vector<node*>  next;
  21.                                 };
  22.                                
  23.                 class graph {
  24.                         private:
  25.                                
  26.                                 int count;
  27.                                 vector<node> * nodes;
  28.                         public:
  29.                                 list <pair<int,int> > F; // forward - wyprzedzająca
  30.                                 list <pair<int,int> > B; // backward - wsteczna
  31.                                 list <pair<int,int> > T; // tree - drzewowa
  32.                                 list <pair<int,int> > C;// crossing - poprzeczna      
  33.                         graph(int numOfNods) {  // konstruktor
  34.                                 visited = new vector<int>(numOfNods);
  35.                                 count = numOfNods;
  36.                                 nodes  = new vector<node>(numOfNods);
  37.                                 timeT = new vector<int>(numOfNods);
  38.                                 for(int i=0;i<numOfNods;i++){   //numerujemy wierzcholki
  39.                                         (*nodes)[i].d=i+1;
  40.                                 }
  41.                                 for(int i=0; i<count;i++){
  42.                                     (*visited)[i]==0;    //malujemy graf na bialo  (nieodwiedzone)
  43.                                 }
  44.                         }
  45.                         ~graph(){       //destruktor
  46.                                 delete nodes;
  47.                                 delete visited;
  48.                                 delete timeT;
  49.                         }
  50.                
  51.                         int first(int v){
  52.                                 v=v-1;
  53.                                 if(v<count && v>= 0)
  54.                                 {
  55.                                                 if((*nodes)[v].next.size()<1) return -1;
  56.                                         if(((*nodes)[v].next[0]) != NULL)
  57.                                         {
  58.                                                 return (*(*nodes)[v].next[0]).d;
  59.                                         }
  60.                                 }      
  61.                                         return -1;
  62.                         }
  63.                        
  64.                         int next(int v, int i){
  65.                                 i=i-1;
  66.                                 if(v-1>=count || v-1<0 ){
  67.                                         return -1;
  68.                                 }
  69.                                 node * n  = &(*nodes)[v-1];
  70.                                 if((n->next.size())>i+1){
  71.                                     node * next= (n->next[i+1]);
  72.                                     return next->d;
  73.                                 }
  74.                                 return -1;
  75.                         }
  76.                        
  77.                        
  78.                         node* vertex(int v, int i){
  79.                                     v=v-1;
  80.                                 node  n  = (*nodes)[v];
  81.                                 for(int j=0;j<n.next.size();j++){
  82.                                         if((*n.next[j]).d==i) return n.next[j];
  83.                                                                 }
  84.                                         return NULL;
  85.                                                 }
  86.                        
  87.                         void setEdge(int v, int i)
  88.                         {
  89.                                 v=v-1;
  90.                             i=i-1;
  91.                             (*nodes)[v].next.push_back(&(*nodes)[i]);
  92.                         }
  93.                        
  94.                          vector<node>  getNodes(){
  95.                             return *nodes;
  96.                         }
  97.                        
  98.                         void printF(){
  99.                             cout<<"Krawedzie wyprzedzajace: "<<endl;
  100.                             for (list <pair<int,int> >::iterator it=F.begin(); it != F.end(); ++it)
  101.                                 cout << ' '<< get<0>(*it)<<" -> "<<get<1>(*it)<<endl;
  102.                         }
  103.                         void printT(){
  104.                             cout<<"Krawedzie drzewowe: "<<endl;
  105.                             for (list <pair<int,int> >::iterator it=T.begin(); it != T.end(); ++it)
  106.                                 cout << ' '<< get<0>(*it)<<" -> "<<get<1>(*it)<<endl;
  107.                         }
  108.                         void printB(){
  109.                             cout<<"Krawedzie wsteczne: "<<endl;
  110.                             for (list <pair<int,int> >::iterator it=B.begin(); it != B.end(); ++it)
  111.                                 cout << ' '<< get<0>(*it)<<" -> "<<get<1>(*it)<<endl;
  112.                         }
  113.                         void printC(){
  114.                             cout<<"Krawedzie poprzeczne: "<<endl;
  115.                             for (list <pair<int,int> >::iterator it=C.begin(); it != C.end(); ++it)
  116.                                 cout << ' '<< get<0>(*it)<<" -> "<<get<1>(*it)<<endl;
  117.                         }        
  118.                 };     
  119.            
  120.              
  121.            
  122.            
  123. // przeglądanie DFS:
  124.    
  125.         void DFS(int v,graph * g){
  126.             //cout<<"dfs :"<<v<<"\n";
  127.             v=v-1;
  128.             (*visited)[v] = 1; //kolorujemy na szaro
  129.             (*timeT)[v]=++time;
  130.             vector<node> nodes = g->getNodes();
  131.             for(int i=0;i<nodes[v].next.size();i++){
  132.                
  133.                 node * next = (nodes[v].next[i]);
  134.                
  135.                 if (next!=NULL)  { //przegladamy jaki jest kolor sasiedniego wierzcholka
  136.                
  137.                     if((*visited)[next->d -1]==0){
  138.                         g->T.push_back(pair<int,int>(v+1, next->d));   // drzewowa
  139.                         DFS(next->d,g);
  140.                        
  141.                     }
  142.                     else if ((*visited)[next->d -1]==1){
  143.                             g->B.push_back(pair<int,int>(v+1, next->d));  //wsteczna
  144.                        
  145.                     }
  146.                     else if ((*visited)[next->d -1] ==2 ){  //czarny, odwiedzony
  147.                            
  148.                             if((*timeT)[next->d -1] > (*timeT)[v]){
  149.                                 g->F.push_back(pair<int,int>(v+1, next->d));
  150.                                
  151.                             }
  152.                             else {
  153.                                 g->C.push_back(pair<int,int>(v+1, next->d));
  154.                            
  155.                             }
  156.                                            
  157.                     }
  158.                        
  159.                 }
  160.                
  161.             }
  162.             (*visited)[v]=2;
  163.             //cout<<"wyszlo dfs "<<v+1<<"\n";
  164.         }
  165.        
  166.        
  167.          
  168. int main(int argc, char** argv) {
  169.        
  170.         int nodeCount = 0;
  171.         int i=0,v = 0;
  172.         cout << "Podaj liczbe wierzcholkow: ";
  173.         cin >> nodeCount ;
  174.         graph g(nodeCount);
  175.         cout << "Podaj krawedzie pomiedzy wierzcholkami (dwie liczby oddzielone spacja)."<< endl;
  176.         cout << "Aby zakonczyc wpisywanie podaj wierzcholek 0 "<< endl;
  177.         do{
  178.                 cin>>v;
  179.                 if(v!=0){
  180.                     cin>>i;
  181.                     g.setEdge(v,i);
  182.                 }
  183.         }while( v != 0 );
  184.        
  185.         cout << "Wierzcholki grafu maja sasiadow: "<<endl;
  186.         for (int i = 1; i<= nodeCount; i++)
  187.         {
  188.                 cout << "wierzcholek nr " << i << " ma sasiadow: ";
  189.                 int j=1;
  190.                 if(g.first(i) != -1) {
  191.                                 cout<<g.first(i)<<" ";
  192.                         while ( g.next(i, j) != -1)
  193.                         {
  194.                                 cout << g.next(i, j) << " " ;
  195.                                 j++;  
  196.                         }
  197.                        
  198.                 }    
  199.                                 cout<< endl;          
  200.         }
  201.         cout << "Przegladanie grafu od wierzcholka numer 1: "<< endl;
  202.        DFS(1,&g);
  203.        g.printT();
  204.        g.printB();
  205.        g.printC();
  206.        g.printF();
  207.        
  208.        
  209.         cout << "Przegladanie grafu od wierzcholka numer 6: "<< endl;
  210.        DFS(6,&g);
  211.        g.printT();
  212.        g.printB();
  213.        g.printC();
  214.        g.printF();
  215.        
  216.         return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement