Stanford Algorithms: Design and Analysis, Part 1[week 4]

Problem Set-4






Programming Question-4


Question 1

Download the text file here. Zipped version here. (Right click and save link as)

The file contains the edges of a directed graph. Vertices are labeled as positive integers from 1 to 875714. Every row indicates an edge, the vertex label in first column is the tail and the vertex label in second column is the head (recall the graph is directed, and the edges are directed from the first column vertex to the second column vertex). So for example, the  11th  row looks liks : "2 47646". This just means that the vertex with label 2 has an outgoing edge to the vertex with label 47646

Your task is to code up the algorithm from the video lectures for computing strongly connected components (SCCs), and to run this algorithm on the given graph. 

Output Format: You should output the sizes of the 5 largest SCCs in the given graph, in decreasing order of sizes, separated by commas (avoid any spaces). So if your algorithm computes the sizes of the five largest SCCs to be 500, 400, 300, 200 and 100, then your answer should be "500,400,300,200,100". If your algorithm finds less than 5 SCCs, then write 0 for the remaining terms. Thus, if your algorithm computes only 3 SCCs whose sizes are 400, 300, and 100, then your answer should be "400,300,100,0,0".

WARNING: This is the most challenging programming assignment of the course. Because of the size of the graph you may have to manage memory carefully. The best way to do this depends on your programming language and environment, and we strongly suggest that you exchange tips for doing this on the discussion forums.

#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#define N 875714
using namespace std;

struct node{
        bool visited;
        int leader;
        int finish;
        vector<int> linkedVertices;
        vector<int> rLinkedVertices;
};

struct node G[N+1];
struct node newG[N+1];//存放修改后的图,下面有问题要问到这个

int t=0;
int s=0;
void dfs(node G[], int i, bool reverse){
        G[i].visited=true;
        G[i].leader=s;
        vector<int> next;
        if (reverse) next= G[i].rLinkedVertices;
        else next= G[i].linkedVertices;
        for(int j=0; j<next.size(); j++){
                if(!G[next[j]].visited) {
                        dfs(G, next[j], reverse);
                }
        }
        t++;
        G[i].finish=t;        
}

void dfs_loop(node G[], bool reverse){
        t=0;
        s=0;
        for(int i=N;i>0;--i){
                if (!G[i].visited){
                        s=i;
                        dfs(G,i,reverse);
                }
        }
}

int main(){
        for(int i=1;i<=N;++i){
                G[i].visited=false;
        }

        FILE* fp=freopen("SCC.txt","r",stdin);
        int head, tail;
        while (scanf("%d %d", &head, &tail) > 0) {
                      G[head].linkedVertices.push_back(tail);
                      G[tail].rLinkedVertices.push_back(head);
        }
        fclose(fp);
        
        dfs_loop(G, true);//FIRST LOOP
        
        for(int i=1;i<=N;++i){
                newG[i].visited=false;
                newG[i].finish=0;
                newG[i].leader=0;
                vector<int> temp;
                for(int j=0; j< G[i].linkedVertices.size(); j++){
                        temp.push_back(G[G[i].linkedVertices[j]].finish);
                }
                newG[G[i].finish].linkedVertices=temp;
        }
        
        dfs_loop(newG,false);//SECOND LOOP
        
        ofstream ofs("stat.txt", ofstream::out);        
        for (int k=1;k<=N;k++) ofs<< newG[k].leader<<endl;
        ofs.close();
        return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值