Skip to content

added graph data structure #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions Graph/Graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package Graph

import (
"errors"
"sync"
)

var (
// ErrVertexNotFound is returned when an operation is requested on a
// non-existent vertex.
ErrVertexNotFound = errors.New("vertex not found")

// ErrSelfLoop is returned when an operation tries to create a disallowed
// self loop.
ErrSelfLoop = errors.New("self loops not permitted")

// ErrParallelEdge is returned when an operation tries to create a
// disallowed parallel edge.
ErrParallelEdge = errors.New("parallel edges are not permitted")
)

// SimpleGraph is a mutable, non-persistent undirected graph.
// Parallel edges and self-loops are not permitted.
// Additional description: https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Simple_graph
type SimpleGraph struct {
mutex sync.RWMutex
adjacencyList map[interface{}]map[interface{}]struct{}
v, e int
}

// V returns the number of vertices in the SimpleGraph
func (g *SimpleGraph) V() int {
g.mutex.RLock()
defer g.mutex.RUnlock()

return g.v
}

// E returns the number of edges in the SimpleGraph
func (g *SimpleGraph) E() int {
g.mutex.RLock()
defer g.mutex.RUnlock()

return g.e
}

// AddEdge will create an edge between vertices v and w
func (g *SimpleGraph) AddEdge(v, w interface{}) error {
g.mutex.Lock()
defer g.mutex.Unlock()

if v == w {
return ErrSelfLoop
}

g.addVertex(v)
g.addVertex(w)

if _, ok := g.adjacencyList[v][w]; ok {
return ErrParallelEdge
}

g.adjacencyList[v][w] = struct{}{}
g.adjacencyList[w][v] = struct{}{}
g.e++
return nil
}

// Adj returns the list of all vertices connected to v
func (g *SimpleGraph) Adj(v interface{}) ([]interface{}, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()

deg, err := g.Degree(v)
if err != nil {
return nil, ErrVertexNotFound
}

adj := make([]interface{}, deg)
i := 0
for key := range g.adjacencyList[v] {
adj[i] = key
i++
}
return adj, nil
}

// Degree returns the number of vertices connected to v
func (g *SimpleGraph) Degree(v interface{}) (int, error) {
g.mutex.RLock()
defer g.mutex.RUnlock()

val, ok := g.adjacencyList[v]
if !ok {
return 0, ErrVertexNotFound
}
return len(val), nil
}

func (g *SimpleGraph) addVertex(v interface{}) {
mm, ok := g.adjacencyList[v]
if !ok {
mm = make(map[interface{}]struct{})
g.adjacencyList[v] = mm
g.v++
}
}

// NewSimpleGraph creates and returns a SimpleGraph
func NewSimpleGraph() *SimpleGraph {
return &SimpleGraph{
adjacencyList: make(map[interface{}]map[interface{}]struct{}),
v: 0,
e: 0,
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ There are several data structures and algorithms implemented in this project. Th
- Binary Tree
- Hash Table
- Trie
- Graph

##### Searching algorithms
- Linear Search
Expand Down