How to implement Karger's Algorithm. Implemented the Graph DS so far. Need help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>

using namespace std;

//adjacency list representation. I have created a list of nodes and edges
class graph
{

private:
    bool *color;
    int vertices;
    void addVertex(int u, int v)
    {
        node *temp=&ptr[u];
        while(temp->next!=NULL)
            temp=temp->next;
        temp->next=new node(v);
    }
public:

    class node
    {
    public:
        node(int name)
        {
            label=name;
            next=NULL;
        };

        node(){next=NULL;};

        int label;

        node *next;
    };

    class edge
    {
    public:
        edge(int u, int v)
        {
            U=u;
            V=v;
            next=NULL;
        }

        int U,V;

        edge *next;
    };

    node *ptr;

    edge *edgeList;
    edge *edgeTemp;
    graph(int V)
    {
        ptr=new node[V+1];
        vertices=V;
        for(int i=1;i<=V;++i)
            ptr[i].label=i;

        color=new bool[vertices+1];

        for(int i=0;i<=vertices;++i)
            color[i]=false;

        edgeList=NULL;
    }

    void addEdge(int u, int v)
    {
        addVertex(u,v);
        addVertex(v,u);

        if(!edgeList)
        {
            edgeList=new edge(u,v);
            edgeTemp=edgeList;
        }

        else
        {
            edgeTemp->next=new edge(u,v);
            edgeTemp=edgeTemp->next;
        }

    }

    void printEdges()
    {
        edge *temp=edgeList;


        while(temp)
        {
            cout<<temp->U<<"-"<<temp->V<<endl;
            temp=temp->next;
        }
    }

    void dfs(int vertex)
    {

        cout<<vertex<<" ";
        color[vertex]=1;

        node *temp=&ptr[vertex];

        while(temp->next)
        {
            temp=temp->next;
            vertex=temp->label;
            if(!color[vertex])
                dfs(vertex);

        }
    }
};

int main()
{
    graph Graph(8);
    Graph.addEdge(1,2);
    Graph.addEdge(1,3);
    Graph.addEdge(2,4);
    Graph.addEdge(2,6);
    Graph.addEdge(2,5);
    Graph.addEdge(3,7);
    Graph.addEdge(3,8);

    Graph.printEdges();
    return 0x0;
}



Please give me some hint regarding how to proceed with the Karger's Algorithm.
Also, is my code for the Graph DS okay? How would professional code this up?
Topic archived. No new replies allowed.