Skip to content

Commit

Permalink
Added graph data structure and DFS algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatgupta310198 committed Sep 14, 2020
1 parent e5da6fa commit 5656851
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
9 changes: 4 additions & 5 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main
// import formatted I/O package
import (
"algos/src/Algo/other/permutaions"
"algos/src/Algo/search/graph"
"fmt"
)

Expand All @@ -21,7 +22,7 @@ func compareA(a float64, b float64) bool {


// Any program in go start with main function inside "main' package
// You can put main inside any other folders like src/main/main.go
// You can put main inside any other folders like src/main/graph_test.go
func main() {
fmt.Printf("Hello World!\n")

Expand Down Expand Up @@ -58,4 +59,20 @@ func main() {
stringVar := "abcd"
permutationStrings := permutaions.HeapsPermutation(4, stringVar)
fmt.Println(permutationStrings)


G := graph.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)
}
73 changes: 73 additions & 0 deletions src/Algo/search/graph/graph.go
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")
}
}
23 changes: 23 additions & 0 deletions src/Algo/search/graph/graph_test.go
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)

}

0 comments on commit 5656851

Please sign in to comment.