图算法《c++算法》(一)

//  DenseGRAPH.cpp : Defines the entry point for the console application.
//

#include 
" stdafx.h "
#include 
< iostream >
#include 
< vector >
#include 
< queue >
using   namespace  std;

class  DenseGRAPH
{
private:

    vector
<vector<bool> > adj;
    
int Vcnt;
    
int Ecnt;
    
bool digraph;
    vector
<bool>visited;
    vector
<int>degree;
    vector
<int>ord;
    vector
<int>id;
    vector
<int>st;
    
int i;
    
int cnt;
    
int ccnt;
    
int index_v;

    
struct Edge//
    {
        
int v;
        
int w;
        Edge(
int v = -1int w = -1):v(v),w(w){}
    }
;
    
public:
    DenseGRAPH(
int V, bool digraph_ = false)
    
{   
        adj.resize(V);
        visited.resize(V, 
false);
        degree.resize(V, 
0);
        ord.resize(V, 
-1);
        id.resize(V, 
-1);
        st.resize(V, 
-1);
        Vcnt 
= V;
        Ecnt 
= 0;
        digraph 
= digraph_;
        i 
= 0;
        cnt 
= 0;
        ccnt 
= 0;
        Edge(
-1-1);
    
        index_v 
= 0;
    
        
for(int m = 0; m < V; m++)
            adj[m].assign(V, 
false);
    }

    
int   V() const return Vcnt; }
    
int   E() const return Ecnt; }
    
bool  directed() const return digraph; }
    
void  insert(Edge e)
    
{
        
int v = e.v, w = e.w;
        
if(false == adj[v][w])
        Ecnt
++;
        adj[v][w] 
= true;
        
if(!digraph)
        adj[w][v] 
= true;
    }

    
void  remove(Edge e)
    
{
        
int v = e.v, w = e.w;
        
if(true == adj[v][w])
        Ecnt
--;
        adj[v][w] 
= false;
        
if(!digraph)
        adj[w][v] 
= false;
    }

    
bool edge(int v, int w) const return adj[v][w]; }

    
int  begin(){ i = -1return next(); }
    
int  next()
    
{
        
for(i++; i < V(); i++)
            
if(adj[index_v][i] == true)
                
return i;
            
return -1;
    }

    
bool end()return i >= V(); }
    
void DEGREE()
    
{
        
for( index_v = 0; index_v < V(); index_v++)
            
for(int w = begin(); !end(); w = next())
                degree[index_v]
++;
    }

    
int operator[](int index_v) constreturn degree[index_v]; }
    
//简单路径:顶点和边均不重复。回路或环仅为首顶点和末顶点重合的简单路径。
   bool searchR(int index, int w)//简单路径搜索
   {   
       index_v 
= index;
       
if(index_v == w) return true;
       visited[index_v] 
= true;
       
for(int t = begin(); !end(); t = next())
           
if(!visited[t])
               
if(searchR(t, w))
                   
return true;
        
return false;
   }

   
//给定两个顶点,是否有一条将其连接的简单路径,而且该路径访问了图中的每个顶点,
   
//且仅访问一次?如果此路径由每个顶点出发由回到该顶点,这个问题就称为哈密顿周游路径。
   
//《c++算法》p47 效率为指数级,极低。。。
   bool H_searchR(int index, int w, int d)//哈密顿路径d=V()-1
   {   
       index_v 
= index;
       
if(index_v == w) return (d == 0);
       visited[index_v] 
= true;
       
for(int t = begin(); !end(); t = next())
           
if(!visited[t])
               
if(H_searchR(t, w, d-1)) return true;
        visited[index_v] 
= false;
        
return false;
   }

   
//是否存在一条路径连接了两个给定顶点,同时用到了图中的每一条边一次且仅用到一次呢?
   
//这条路径不必是简单路径,而顶点可以被访问多次。
   bool E_Path(int index, int w)//欧拉路径的存在性
   {   
       index_v 
= index;
       
bool found;
       
int t = degree[index_v] + degree[w];
       
if((t % 2!= 0){ found = falsereturn false; }
       
for(t = 0; t <V(); t++)
           
if((t!= index_v) && (t != w))
               
if((degree[t] % 2!= 0)
               
{ found = falsereturn false;}
        found 
= true;
   }

    
void DFS(Edge e)//深度优先搜索
    {
        
int w = e.w;
        ord[w] 
= cnt++;
        st[e.w] 
= e.v;
        
for(int t = begin(); !end(); t = next())
            
if(-1 == ord[t])DFS(Edge(w, t));
    }

    
void DFS_search()
    
{
        
for(int index_v = 0; index_v < V(); index_v++)
            
if(-1 == ord[index_v])DFS(Edge(index_v, index_v));
    }

   
void ccR(int w)
   
{  
       id[w] 
= ccnt;
        
for(index_v = begin(); !end(); index_v = next())
           
if(-1 == id[index_v]) ccR(index_v);
   }

   
void count_ccR()
   
{
       
for( index_v = 0; index_v < V(); index_v++)
           
if(-1 == id[index_v])
           
{
               ccR(index_v);
               ccnt
++;
           }

   }

   
bool connect(int s, int t) const//看是否联通,首先得调用count_ccR()
   {
       
return id[s] == id[t];
   }

   
void BFS(Edge e)//广度优先搜索
   
       queue
<Edge>Q;
       Q.push(e);
       
while(!Q.empty())
       
{
           
if(-1 == ord[(e = Q.front()).w] )//vc6.0不支持标准库,无top()成员函数
           {
               
int v = e.v;
               
int w = e.w;
               ord[w] 
= cnt++;
               st[w] 
= v;
               Q.pop();
               
for(int t = begin(); !end(); t = next())
                   
if(-1 == ord[t])
                       Q.push(Edge(w, t));
           }

       }

   }

Edge Get_Edge(
int x,  int y)
{    
    Edge m(x, y);
    
return m;
}

}
;
int  main( int  argc,  char *  argv[])
{

    DenseGRAPH ooo(
20,false);
    ooo.insert(ooo.Get_Edge(
2,3));
//    ooo.insert(ooo.Get_Edge(5,4));
    ooo.insert(ooo.Get_Edge(3,4));
    ooo.insert(ooo.Get_Edge(
1,2));
    ooo.insert(ooo.Get_Edge(
2,4));
//    ooo.insert(ooo.Get_Edge(10,5));
    ooo.insert(ooo.Get_Edge(6,7));
    ooo.insert(ooo.Get_Edge(
5,7));
    ooo.insert(ooo.Get_Edge(
5,6));
//    cout<<ooo.E()<<endl;
//    ooo.remove(ooo.Get_Edge(10,5));
//    cout<<ooo.E()<<endl;
//    ooo.insert(ooo.Get_Edge(3,7));
//    ooo.insert(ooo.Get_Edge(7,8));
//    ooo.insert(ooo.Get_Edge(2,8));
//    ooo.DEGREE();
//    cout<<ooo.operator[](7)<<endl;
//    cout<<ooo.operator [](4)<<endl;
//    cout<<ooo.searchR(1,4)<<endl;
    cout<<ooo.H_searchR(176)<<endl;
    ooo.DFS_search();
    ooo.count_ccR();
    cout
<<ooo.connect(1,5)<<endl;
    
return 0;
}

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yshuise

权术横行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值