算法课4-图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/srping123/article/details/83475163

表示方法分为邻接矩阵和邻接表 推荐邻接表
BFS DFS是基础
以下基于邻接表BFS 和 DFS转自https://blog.csdn.net/LaoJiu_/article/details/50389860

BFS用队列 DFS用递归或栈

#include <iostream>
#include <iostream>
#include <queue>
#include <string.h>
#include <stack>



/*
 * 邻接表实现DFS 和 BFS
 * 请自行手打复习,考虑边表、顶点表、图结构
 *
 */

using namespace std;


struct ArcNode //边表
{
    int adjvex;               //该弧所指向的顶点的位置
    ArcNode * next;           //指向下一条弧的指针
    //int weight;边上是否有权
};

typedef struct VNode//顶点表
{
    char vertex;              //顶点信息
    ArcNode * firstarc;       //指向第一条依附该顶点的弧的指针 即该顶点第一条指出去的边
}AdjList[20];

struct ALGraph //图
{
    AdjList adjList;
    int vexNum;               //图的顶点数
    int arcNum;               //图的弧数
};

int visited[20];//设置标志数组

void CreateGraph(ALGraph & graph){// //此处函数的形参graph都是引用 传递引用给函数与传递指针的效果是一样的 https://blog.csdn.net/love_gaohz/article/details/7517891
    ////////1.输入顶点数和弧数
    cout << "请输入图的顶点数: ";
    cin >> graph.vexNum;
    cout << "请输入图的弧数: ";
    cin >> graph.arcNum;

    ///////2.输入顶点信息
    cout << "请输入" << graph.vexNum << "个顶点信息: ";
    for (int i = 0; i < graph.vexNum; i++)
    {
        cin >> graph.adjList[i].vertex;
        graph.adjList[i].firstarc = nullptr;
    }

    ///////3.根据输入的弧的信息构造邻接表
    cout << "请输入" << graph.arcNum << "个弧的信息: \n";
    int h1, h2;
    ArcNode * temp;
    for (int i = 0; i < graph.arcNum; i++)
    {
        cin >> h1 >> h2;

        temp = new ArcNode;
        temp->adjvex = h2;
        temp->next = graph.adjList[h1].firstarc;
        graph.adjList[h1].firstarc = temp;

        temp = new ArcNode;
        temp->adjvex = h1;
        temp->next = graph.adjList[h2].firstarc;
        graph.adjList[h2].firstarc = temp;
    }

}
void PrintGraph(ALGraph & graph){
    for(int i=0;i<graph.vexNum;i++){
        cout<<graph.adjList[i].vertex<<"-->";
        ArcNode * firstarc=graph.adjList[i].firstarc;
        while(firstarc){
            cout<<graph.adjList[firstarc->adjvex].vertex;
            firstarc=firstarc->next;
        }
        cout<<endl;

    }
}

void DFS(ALGraph & graph, int v){
    cout << graph.adjList[v].vertex;
    visited[v]=1;
    ArcNode * firstarc=graph.adjList[v].firstarc;
    while(firstarc){
        if(visited[firstarc->adjvex]==-1) {

            DFS(graph,firstarc->adjvex);
        }
        firstarc=firstarc->next;
    }
}

void DFS2(ALGraph & graph,int u)
{

    stack<int> s;
    visited[u]=1;
    s.push(u);

    while(!s.empty())
    {
        int v=s.top();
        s.pop();

        cout<<graph.adjList[u].vertex;
        ArcNode *p=graph.adjList[u].firstarc;
        while(p)
        {
            if(visited[p->adjvex]==-1)
            {
                s.push(p->adjvex);
                visited[p->adjvex]=1;

            }
            p=p->next;
        }

    }
}

void DFSTraverse(ALGraph & graph){
    for(int i=0;i<graph.vexNum;i++){
        visited[i]=-1;
    }
    for(int i=0;i<graph.vexNum;i++){
        if(visited[i]==1)continue;
        DFS2(graph,i);
    }

}




void BFSTraverse(ALGraph & graph){
    for(int i=0;i<graph.vexNum;i++){
        visited[i]=-1;
    }
    queue<int> q;
    for(int i=0;i<graph.vexNum;i++){
        if(visited[i]==1) continue;
        visited[i]=1;
        q.push(i);
        cout<<graph.adjList[i].vertex;
        while(!q.empty()){
            int x=q.front();
            q.pop();
            ArcNode * firstarc=graph.adjList[x].firstarc;
            while(firstarc){
                if(visited[firstarc->adjvex]==-1) {
                    visited[firstarc->adjvex]=1;
                    q.push(firstarc->adjvex);
                    cout << graph.adjList[firstarc->adjvex].vertex;
                }
                firstarc=firstarc->next;
            }

        }
    }
}





int main() {

    ALGraph graph;

    //1.创建邻接表
    CreateGraph(graph);

    //2.打印邻接表
    cout << "\n邻接表打印为: \n";
    PrintGraph(graph);

    //3.深度优先搜索DFS
    cout << "\n深度优先搜索DFS: ";
    DFSTraverse(graph);
    cout << endl;

    //4.广度优先搜索BFS
    cout << "\n广度优先搜索BFS: ";
    BFSTraverse(graph);
    cout << endl<<endl;

    return 0;
}

第二次上机作业,二部图匹配
首先,leetcode https://leetcode.com/problems/is-graph-bipartite/
使用BFS 涂色的思路
判断一个图是否为二部图
上机作业第二题 B:Butterfly
总时间限制: 1000ms 单个测试点时间限制: 100ms 内存限制: 65536kB
描述
有一群旅行爱好者,有一天,他们带回了n只蝴蝶回来。他们相信每一只都属于两个不同种类中的一种,为了讨论方便,我们称它们为A与B。他们想把n只标本分成两组——一些属于A且一些属于B——但是直接标记任何一个标本对于他们是非常困难,因此他们决定采用下面的方法。

对每对标本i和j,他们细心地把它们放到一起研究。如果他们以自己的判断足以确信,那么他们把这对蝴蝶标记为“相同”(这意味着他们相信这两只来自同一类)或者是“不同”(这意味着他们相信这两只来自不同的类)。他们也可以对某些标本判断不出来而弃权,在这种情况下,我们称这对标本是不明确的。

现在他们有n只标本的集合,还有对那些没有弃权的标本对的m个判断的集合(“相同”或者“不同”)。他们想知道这个数据与每只蝴蝶来自A和B中的一个类的想法是否一致。更具体地说,如果对每对蝴蝶按照下述方式标记A或B是可能的,即对每个标为“相同”的(i,j)对,就是i与j有相同标记的情况;对每个标为“不同”的(i,j)对,就是i与j有不同标记的情况。那么我们可以说这m个判断是一致的。他们正在冥思苦想自己的判断是否是一致的。请你帮他们设计合理的算法解决该问题。

输入
输入包含多组数据,以文件结束符为终止。

每组数据第一行为两个整数,分别是n和m:
n为蝴蝶的数量,编号从0到n-1
m为关系的数量

接下来是m组关系数据,每组数据占一行,为三个整数,前两个整数表示蝴蝶的编号,第三个整数为关系的种类(相同或者不同):
0为相同,1为不同

1 < n <= 1000
1 < m <= 100000

输出
合理就输出YES
不合理就输出NO
样例输入

3 3
0 1 0
1 2 1
0 2 1
3 3
0 1 0
1 2 1
0 2 0

样例输出

YES
NO

#include<iostream>
#include<string>
#include<string.h>
#include<queue>
#include<functional>
#include<vector>

using namespace std;

int n, m;
int relation[1005][1005];//表示输入蝴蝶的关系
int vis[1005];//用于标记是否已经寻找过该关系
int color[1005];//用于存储是每只蝴蝶的颜色
int index1,index2;

queue<int> q;


bool bfs(int node){
    if(vis[node]){
        return true;
    }
    vis[node]=1;
    if(color[node]==0){
        color[node]=1;
    }
    for(int j=0;j<n;j++){
        if(relation[node][j]==1){
            if(color[j]==0){
                color[j]=3-color[node]; //小trick
                q.push(j);
            }
            else if(color[j]==color[node]){
                return false;
            }
        }
        else if(relation[node][j]==0){
            if(color[j]==0){
                color[j]=color[node];
                q.push(j);
            }
            else if (color[node] != color[j])
            {
                return false;
            }
        }
    }
    return true;
}


int main()
{
    bool res=true;
    while(~scanf("%d %d",&n,&m)) //遇到EOF 
    {
        memset(relation, -1, sizeof(relation));
        memset(color, 0, sizeof(color));
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < m; i++) {
            int re;
            scanf("%d%d%d",&index1,&index2,&re);
            relation[index1][index2]=re;
//            relation[index1][index2] = 2 - re;//1为不同,2为相同
//            relation[index2][index1] = 2 - re;
        }
        for(int k=0;k<n;k++){
            q.push(k);
            while(!q.empty()){
                if(!bfs(q.front())){
                    res=false;
                    break;
                }
                q.pop();
            }
        }
        if(res){
            cout <<"YES"<< endl;
        }
        else{
            cout<<"NO"<<endl;
        }

    }

    return 0;
}

一些例子
flood fill https://leetcode.com/problems/flood-fill/
判断一个图中是否有环 思路 1.拓扑排序 2. dfs
https://blog.csdn.net/login_sonata/article/details/78002042
https://www.jianshu.com/p/acbd585f5c60

猜你喜欢

转载自blog.csdn.net/srping123/article/details/83475163
今日推荐