• BFS-C


    //
    //  main.c
    //  BFS
    //
    //  Created by 韩雪滢 on 10/23/16.
    //  Copyright © 2016 韩雪滢. All rights reserved.
    //
    
    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAX 100
    
    #define initial 1
    #define waiting 2
    #define visited 3
    
    int n;
    int adj[MAX][MAX];
    int state[MAX];
    void create_graph();
    void BF_Traversal();
    void BFS(int v);
    
    int queue[MAX], front = -1,rear = -1;
    void insert_queue(int vertex);
    int delete_queue();
    int isEmpty_queue();
    
    int main()
    {
        create_graph();
        BF_Traversal();
        return 0;
    }
    
    void BF_Traversal()
    {
        int v;
        
        for(v=0; v<n; v++)
            state[v] = initial;
        
        printf("Enter Start Vertex for BFS: 
    ");
        scanf("%d", &v);
        BFS(v);
    }
    
    void BFS(int v)
    {
        int i;
        
        insert_queue(v);
        state[v] = waiting;
        
        while(!isEmpty_queue())
        {
            v = delete_queue( );
            printf("%d ",v);
            state[v] = visited;
            
            for(i=0; i<n; i++)
            {
                if(adj[v][i] == 1 && state[i] == initial)
                {
                    insert_queue(i);
                    state[i] = waiting;
                }
            }
        }
        printf("
    ");
    }
    
    void insert_queue(int vertex)
    {
        if(rear == MAX-1)
            printf("Queue Overflow
    ");
        else
        {
            if(front == -1)
                front = 0;
            rear = rear+1;
            queue[rear] = vertex ;
        }
    }
    
    int isEmpty_queue()
    {
        if(front == -1 || front > rear)
            return 1;
        else
            return 0;
    }
    
    int delete_queue()
    {
        int delete_item;
        if(front == -1 || front > rear)
        {
            printf("Queue Underflow
    ");
            exit(1);
        }
        
        delete_item = queue[front];
        front = front+1;
        return delete_item;
    }
    
    void create_graph()
    {
        int count,max_edge,origin,destin;
        
        printf("Enter number of vertices : ");
        scanf("%d",&n);
        max_edge = n*(n-1);
        
        for(count=1; count<=max_edge; count++)
        {
            printf("Enter edge %d( -1 -1 to quit ) : ",count);
            scanf("%d %d",&origin,&destin);
            
            if((origin == -1) && (destin == -1))
                break;
            
            if(origin>=n || destin>=n || origin<0 || destin<0)
            {
                printf("Invalid edge!
    ");
                count--;
            }
            else
            {
                adj[origin][destin] = 1;
            }
        }
    }
  • 相关阅读:
    进程的理论基础
    笔记
    ssh socketserver
    redis基本类型和操作
    centos redis 安装
    spring data jpa 查询部分字段
    spring data jpa 查询部分字段列名无效问题
    多个ajax执行混乱问题
    spring data jpa使用原生sql查询
    idea修改变量及其引用
  • 原文地址:https://www.cnblogs.com/HackHer/p/5989289.html
Copyright © 2020-2023  润新知