设为首页 加入收藏

TOP

图的深度优先搜索和广度优先搜索算法、最小生成树两种算法 --C++实现(一)
2016-08-20 17:03:23 】 浏览:478
Tags:深度 优先 搜索 广度 算法 最小 生成 --C 实现

一:通用图结构

#ifndef _GRAPH_H
#define _GRAPH_H

#include 
   
    
#include 
    
      #include 
     
       #include 
      
        using namespace::std; #define MAX_COST 0x7FFFFFFF //花费无限大设为整型最大值 /////////////////////////////////////////////////////////////////////////////////////////// //通用图结构 template 
       
         class graph{ public: bool is_empty()const; bool is_full()const; int get_numvertices()const; //当前顶点数 int get_numedges()const; //当前边数 public: virtual bool insert_vertex(const T&) = 0; //插入顶点 virtual bool insert_edge(const T&, const T&, E) = 0; //插入边 virtual int get_firstneighbor(const T&)const = 0; //得到第一个邻接顶点 virtual int get_nextneighbor(const T&, const T&)const = 0; //某邻接顶点的下一个邻接顶点 virtual void print_graph()const = 0; virtual int get_vertex_index(const T&)const = 0; //得到顶点序号 virtual void depth_first(const T&) = 0; virtual void broad_first(const T&) = 0; virtual void min_spantree_kruskal() = 0; virtual void min_spantree_prim(const T&) = 0; protected: static const int VERTICES_DEFAULT_SIZE = 10; //默认图顶点数 int max_vertices; int num_vertices; int num_edges; }; template 
        
          bool graph
         
          ::is_empty()const { return num_edges == 0; } template 
          
            bool graph
           
            ::is_full()const { return num_vertices >= max_vertices || num_edges >= max_vertices*(max_vertices-1)/2; //判满,分为顶点满和边满 } template 
            
              int graph
             
              ::get_numvertices()const { return num_vertices; } template 
              
                int graph
               
                ::get_numedges()const { return num_edges; } /////////////////////////////////////////////////////////////////////////////////////////// #define VERTICES_DEFAULT_SIZE graph
                
                 ::VERTICES_DEFAULT_SIZE #define num_vertices graph
                 
                  ::num_vertices #define num_edges graph
                  
                   ::num_edges #define max_vertices graph
                   
                    ::max_vertices /////////////////////////////////////////////////////////////////////////////////////////// #endif /*graph.h*/ 
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   

二:邻接矩阵图结构

#pragma once

#include "graph.h"

//图的邻接矩阵表示法
template 
    
     
class graph_mtx : public graph
     
      { public: graph_mtx(int); ~graph_mtx(); public: bool insert_vertex(const T&); bool insert_edge(const T&, const T&, E); int get_firstneighbor(const T&)const; int get_nextneighbor(const T&, const T&)const; int get_vertex_index(const T&)const; T& get_vertex_symbol(const int)const; void print_graph()const; void depth_first(const T&); void broad_first(const T&); void min_spantree_kruskal(); void min_spantree_prim(const T&); protected: void depth_first(const T&, bool *); private: T* vertices_list; //顶点线性表 E **edge; //内部矩阵 }; template 
      
        graph_mtx
       
        ::graph_mtx(int sz = VERTICES_DEFAULT_SIZE) { max_vertices = sz > VERTICES_DEFAULT_SIZE  sz : VERTICES_DEFAULT_SIZE; vertices_list = new T[max_vertices]; edge = new int*[max_vertices]; //动态申请二维数组 for(int i=0; i
        
          graph_mtx
         
          ::~graph_mtx() { for(int i=0; i
          
            bool graph_mtx
           
            ::insert_vertex(const T& vert) { if(this->is_full()) //派生类函数调用父类函数,用this或加作用域 return false; vertices_list[num_vertices++] = vert; return true; } template 
            
              bool graph_mtx
             
              ::insert_edge(const T& vert1, const T& vert2, E cost = MAX_COST)//由于权值存在默认值,get_neighbor的操作需判断是否等于MAX_COST,否则不能正常取得邻接顶点 { if(this->is_full()) //判满 return false; int index_v1 = get_vertex_index(vert1); //得到顶点序号 int index_v2 = get_vertex_index(vert2); if(index_v1 == -1 || index_v2 == -1 ) return false; edge[index_v1][index_v2] = edge[index_v2][index_v1] = cost; //无向图 ++num_edges; return true; } template 
              
                int graph_mtx
               
                ::get
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【c++知识归纳】继承与多态(一) 下一篇c++11实现一个半同步半异步线程池

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目