-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.h
65 lines (57 loc) · 1.91 KB
/
Graph.h
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
#pragma once
#include<iostream>
#include<cstdlib>
using namespace std;
/*
|Verticies| |<--- adjacency list --->|
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
| | +------+------+------+------+------+------+------+------+
| pointer |-------->| node | node | node | node | node | node | node | node |
| | +------+------+------+------+------+------+------+------+
+---------+
*/
class Graph {
//colors used to detect cycles
enum Color { WHITE, GRAY, BLACK };
//struct for an adjacency list node
private:
class AdjListNode {
public:
AdjListNode(int d);
int data;
AdjListNode* next;
};
//struct for an adjacency list
class AdjList {
public:
AdjListNode* head; //pointer to head node of list
};
//struct for a graph. A graph as an array of adjacency lists
//Size of array will be V (total vertices)
// memeber variables
int V;
AdjList* arr;
public:
Graph(int v = 0);
~Graph();
void addEdge(int, int);
void printGraph();
bool DFSUtil(int v, int color[]);
bool isCyclic();
static void exe(); // user input
//void getEdge(int& src, int& des);
};
ostream& operator <<(ostream& out, Graph& g);
//istream& operator >>(istream& out, Graph& g);