-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added graph data structure and DFS algorithm
- Loading branch information
1 parent
e5da6fa
commit 5656851
Showing
4 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,74 @@ | ||
package graph | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Vertex Structure | ||
type Vertex struct { | ||
Label string | ||
AdjList []*Vertex | ||
} | ||
|
||
// Graph Entity | ||
type Graph struct { | ||
VertexCount int | ||
Vertices []*Vertex | ||
} | ||
|
||
// NewGraph returns | ||
func NewGraph() *Graph { | ||
return new(Graph) | ||
} | ||
|
||
// AddVertex adds new vertex to G of given label and returns that vertex | ||
func (g *Graph) AddVertex(label string) *Vertex { | ||
vertex := new(Vertex) | ||
vertex.Label = label | ||
g.VertexCount = g.VertexCount + 1 | ||
g.Vertices = append(g.Vertices, vertex) | ||
return vertex | ||
} | ||
|
||
// AddEdge creates an edge between two vertex | ||
func (g *Graph) AddEdge(srcVertex, dstVertex *Vertex) *Vertex { | ||
srcVertex.AdjList = append(srcVertex.AdjList, dstVertex) | ||
return srcVertex | ||
} | ||
|
||
// Dfs performs Depth Fist Search | ||
func (g *Graph) Dfs(vertex *Vertex) { | ||
//visited := []bool{false, false} | ||
|
||
visited := make(map[string]bool) | ||
for i := range g.Vertices { | ||
visited[g.Vertices[i].Label] = false | ||
} | ||
g.dfsUtil(vertex, visited) | ||
|
||
} | ||
|
||
// dfsUtil is utility for Dfs | ||
func (g *Graph) dfsUtil(vertex *Vertex, visited map[string]bool) { | ||
|
||
visited[vertex.Label] = true | ||
fmt.Printf("\n%s ", vertex.Label) | ||
for _, v := range vertex.AdjList { | ||
if visited[v.Label] == false { | ||
g.dfsUtil(v, visited) | ||
} | ||
} | ||
|
||
} | ||
|
||
// PrintGraph prints the create graph G | ||
func (g *Graph) PrintGraph() { | ||
fmt.Printf("Number of vertices %d\n", g.VertexCount) | ||
for _, v := range g.Vertices { | ||
fmt.Printf("Vertex: %s\n Adjency list \n", v.Label) | ||
for _, adv := range v.AdjList { | ||
fmt.Printf("%s, ", adv.Label) | ||
} | ||
fmt.Printf("\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
package graph | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNewGraph(t *testing.T) { | ||
|
||
G := NewGraph() | ||
|
||
v0 := G.AddVertex("A") | ||
v1 := G.AddVertex("B") | ||
v2 := G.AddVertex("C") | ||
v3 := G.AddVertex("D") | ||
G.AddEdge(v0, v1) | ||
G.AddEdge(v0, v2) | ||
G.AddEdge(v1, v2) | ||
G.AddEdge(v2, v0) | ||
G.AddEdge(v2, v3) | ||
G.AddEdge(v3, v3) | ||
G.PrintGraph() | ||
G.Dfs(v2) | ||
|
||
} |