使用邻接列表表示为每个顶点计算indegree和outdegree的函数

时间:2015-11-14 10:19:22

标签: c++

我有一个使用邻接列表表示的有向图。但我遇到了麻烦。我想编写一个函数来计算每个顶点的indegree和outdegree。你能建议我用C ++解决吗? 这是我的源代码:

    #include<iostream>
    #include <list>
    using namespace std;

    // A directed graph using adjacency list representation
    class Graph
    {
        int V;    // No. of vertices in graph
        list<int> *adj; // Pointer to an array containing adjacency lists

        // A recursive function used by printAllPaths()
        void printAllPathsUtil(int , int , bool [], int [], int &);

    public:
        Graph(int V);   // Constructor
        void addEdge(int u, int v);
    };

    Graph::Graph(int V)
    {
        this->V = V;
        adj = new list<int>[V];
    }

    void Graph::addEdge(int u, int v)
    {
        adj[u].push_back(v); // Add v to u’s list.
    }

1 个答案:

答案 0 :(得分:1)

只需绘制一张图表,然后自己计算出indegree和outdegree。你会明白你需要做什么。 我会给出提示,以便你可以自己解决

<强>提示-1

Outdegree是一个简单的节点。想一想邻接列表条目包含什么?这就是它的所有节点。得到它了!!

<强>提示-2

在查看SW年龄时,您还会关注NW。因此#include <stdlib.h> #include <stdio.h> #include <string.h> #define N 24 #define die(...) exit((fprintf(stderr, __VA_ARGS__), 1)) enum {NW, NE, SE, SW, NCORNER}; static const int dx[NCORNER] = {0, 1, 1, 0}; static const int dy[NCORNER] = {0, 0, 1, 1}; struct stype { const char *name; int start; int end; const char *path; }; #define STYPE(X) \ X(None, -1, -1, "") \ X(WallN, NW, NE, "E1") \ X(WallE, NE, SE, "S1") \ X(WallS, SE, SW, "W1") \ X(WallW, SW, NW, "N1") \ X(QuarterNE, SE, NW, "W2 SW N2") \ X(QuarterSE, SW, NE, "N2 NW E2") \ X(QuarterSW, NW, SE, "E2 NE S2") \ X(QuarterNW, NE, SW, "S2 SE W2") \ X(SpikeN, NE, NW, "S2 SE SW N2") \ X(SpikeE, SE, NE, "W2 SW NW E2") \ X(SpikeS, SW, SE, "N2 NW NE S2") \ X(SpikeW, NW, SW, "E2 NE SE W2") \ X(Circle, NW, NW, "MM") #define STYPE_STRUCT(Name, S1, S2, P) {#Name, S1, S2, P}, #define STYPE_ENUM(Name, S1, S2, P) Name, enum {STYPE(STYPE_ENUM) MAX_STYPE}; static const struct stype stype[MAX_STYPE] = { STYPE(STYPE_STRUCT)}; int ctype[16][3] = { /* 0 */ {Circle, None}, /* 1 */ {SpikeN, None}, /* 2 */ {SpikeE, None}, /* 3 */ {QuarterNE, None}, /* 4 */ {SpikeS, None}, /* 5 */ {WallW, WallE, None}, /* 6 */ {QuarterSE, None}, /* 7 */ {WallW, None}, /* 8 */ {SpikeW, None}, /* 9 */ {QuarterNW, None}, /* 10 */ {WallN, WallS, None}, /* 11 */ {WallS, None}, /* 12 */ {QuarterSW, None}, /* 13 */ {WallE, None}, /* 14 */ {WallN, None}, /* 15 */ { None}, }; struct seg { int type; int x; int y; }; struct pt { int seg1; int seg2; }; int alive(int cell[N][N], int x, int y) { if (x < 0 || x >= N) return 0; if (y < 0 || y >= N) return 0; return cell[y][x]; } void addpt(struct pt *pt, int seg) { if (pt->seg1 < 0) { pt->seg1 = seg; } else if (pt->seg2 < 0) { pt->seg2 = seg; } else { die("Too many segments for point.\n"); } } int main(void) { int cell[N][N]; int count = 0; int i, x, y; for (y = 0; y < N; y++) { for (x = 0; x < N; x++) { int r = 1 + abs(N/2 - x) + abs(N/2 - y); cell[y][x] = (rand() / 4 < RAND_MAX / r); if (cell[y][x]) count++; } } /* Create line segments */ struct seg seg[2 * count]; int nseg = 0; for (y = 0; y < N; y++) { for (x = 0; x < N; x++) { int ix = 0; if (cell[y][x] == 0) continue; if (alive(cell, x, y - 1)) ix |= 1; if (alive(cell, x + 1, y)) ix |= 2; if (alive(cell, x, y + 1)) ix |= 4; if (alive(cell, x - 1, y)) ix |= 8; int *p = ctype[ix]; while (*p != None) { if (nseg >= 2 * count) die("Segment overflow\n"); seg[nseg].x = x; seg[nseg].y = y; seg[nseg].type = *p++; nseg++; } } } /* determine start and end points of segments */ struct pt pt[N + 1][N + 1]; memset(pt, -1, sizeof(pt)); for (i = 0; i < nseg; i++) { int tp = seg[i].type; int s = stype[tp].start; int e = stype[tp].end; x = seg[i].x; y = seg[i].y; addpt(&pt[y + dy[s]][x + dx[s]], i); addpt(&pt[y + dy[e]][x + dx[e]], i); } /* set up PostScript header */ puts("%!PS-Adobe 3.0"); puts("/A 10 def"); puts("/A2 A 2 mul def"); puts("/C { rcurveto } def"); puts("/L { rlineto } def"); puts("/START { newpath exch A2 mul exch A2 mul moveto } bind def"); puts("/END { closepath stroke } bind def"); puts("/MM { A 0 rmoveto NE SE SW NW } bind def"); puts("/NW { 0 A neg 0 A neg A A neg C } bind def"); puts("/NE { A 0 A 0 A A C } bind def"); puts("/SE { 0 A 0 A A neg A C } bind def"); puts("/SW { A neg 0 A neg 0 A neg A neg C } bind def"); puts("/N1 { 0 A2 neg L } bind def"); puts("/E1 { A2 0 L } bind def"); puts("/S1 { 0 A2 L } bind def"); puts("/W1 { A2 neg 0 L } bind def"); puts("/N2 { 0 A neg L } bind def"); puts("/E2 { A 0 L } bind def"); puts("/S2 { 0 A L } bind def"); puts("/W2 { A neg 0 L } bind def"); puts("57 180 translate"); /* walk segments */ for (i = 0; i < nseg; i++) { struct seg *s = seg + i; if (s->type == None) continue; int x0 = s->x + dx[stype[s->type].start]; int y0 = s->y + dy[stype[s->type].start]; int j = i; x = s->x + dx[stype[s->type].end]; y = s->y + dy[stype[s->type].end]; printf("%d %d START", x0, y0); for (;;) { printf(" %s", stype[s->type].path); s->type = None; if (x == x0 && y == y0) break; if (pt[y][x].seg1 == j) { j = pt[y][x].seg2; } else { j = pt[y][x].seg1; } s = seg + j; x = s->x + dx[stype[s->type].end]; y = s->y + dy[stype[s->type].end]; } puts(" END"); } puts("showpage"); return 0; } 中有一些东西必须促成不确定性。不是吗?遍历所有边缘然后你会知道...

[您可能必须使用数组进行a-->b计算]