-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndirectedGraphDepthFirst.cs
122 lines (104 loc) · 3.17 KB
/
UndirectedGraphDepthFirst.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Algorithms
{
/*
* Runs in O(E+V) time
*/
public class UndirectedGraphDepthFirst
{
//marked will hold all the vertics the vertes has path to.
private bool[] marked;
private int totalPaths;
Queue<int> path;
public UndirectedGraphDepthFirst(UndirectedGraph graph)
{
marked = new bool[graph.getTotalVertices()];
path = new Queue<int>();
totalPaths = 0;
}
public Queue<int> getPath()
{
if (path.Count == 0)
{
return null;
}
return path;
}
//do a depth first search on a vertex to find out all the vertices it has a path to. This also prints out all the vertices
public void search(UndirectedGraph graph, int vertex)
{
totalPaths++;
marked[vertex] = true;
ArrayList adj = graph.Adjacent(vertex);
path.Enqueue(vertex);
//this is how it traverses
//Console.WriteLine(vertex);
for(int i = 0; i< adj.Count; i++)
{
int currentVertex = (int) adj[i];
if (marked[currentVertex] != true)
{
search(graph, currentVertex);
}
}
}
public Boolean pathExists(int v)
{
return marked[v];
}
public Boolean isConnected(UndirectedGraph g)
{
return totalPaths == g.getTotalVertices();
}
public static void main()
{
UndirectedGraph g = new UndirectedGraph(13);
g.AddEdge(0, 6);
g.AddEdge(0, 2);
g.AddEdge(0, 1);
g.AddEdge(0, 5);
g.AddEdge(1, 0);
g.AddEdge(2, 0);
g.AddEdge(3, 5);
g.AddEdge(3, 4);
g.AddEdge(4, 5);
g.AddEdge(4, 6);
g.AddEdge(4, 3);
g.AddEdge(5, 3);
g.AddEdge(5, 4);
g.AddEdge(5, 0);
g.AddEdge(6, 0);
g.AddEdge(6, 4);
g.AddEdge(7, 8);
g.AddEdge(8, 7);
g.AddEdge(9, 11);
g.AddEdge(9, 10);
g.AddEdge(9, 12);
g.AddEdge(10, 9);
g.AddEdge(11, 9);
g.AddEdge(11, 12);
g.AddEdge(12, 11);
g.AddEdge(12, 9);
UndirectedGraphDepthFirst depthFirst = new UndirectedGraphDepthFirst(g);
depthFirst.search(g, 8);
Queue<int> q = depthFirst.getPath();
Console.WriteLine("Path is: ");
foreach (int value in q)
{
Console.Write(value + " ");
}
for(int i = 0; i < g.getTotalEdges(); i++)
{
if (depthFirst.pathExists(i) != false)
{
Console.Write(i);
}
}
Console.WriteLine("Connected: " + depthFirst.isConnected(g));
}
}
}