o
odinpkg.dev
packages / library / Graph-in-Odin

Graph-in-Odin

e1bf70alibrary

A simple port of a graph in C to the Odin programming language. And a good section of several templates.

No license · updated 3 years ago

Graph in Odin

This is a code of a port of graph originally implemented in C to Odin. Adjacency lists have been used to make the graph.
There is a good section on templates in the queue.odin file, several different template ways of doing the same thing, it was the fruit of a really good discussion with the discord members of the Odin forum, my thanks go to them.

Original C code for this port to Odin

Original source code in C can be found at:

Github tharaka27 / graph-c
       Tharaka Ratnayake 
       https://github.com/tharaka27/graph-c

How to use this graph ?

First of all yo have to allocate memory for the graph. which you can do with following.

my_g1, my_g2 : ^Graph_t
my_g1 = nil
my_g2 = nil 
if len( os.args ) < 2 {
    fmt.eprintf( "Usage: %v graph_name" , args[0] )
    os.exit( -1 )
}
my_g1 = new( Graph_t )
defer free( my_g1 )

Then you can use following functions or procedures to implement the graph as you wish. Odin is not a OO - Object Oriented language, but you can use it to write code that is object orient style of coding, like you can use to write in other low level languages like C. You can make a struct for each class of every object you need. With a function constructor (that returns a pointer to the object instantiated) and a function destructor (that clean up every thing), and each other function (like method receiving the this or self parameter, but with the name of the struct ), receiving, as it's first argument the pointer to the object instantiated.
But Odin, has many niceties, it's easy to put inheritance in place and have the equivalent to virtual methods of C Plus Plus, but with functions.
It's a Data Oriented language and not a Object Oriented language, but it's a very fine and clean one. Really a little perl in a see of complex languages, but simple doesn't means that is isn't a very powerful language.

1. Initialize the graph.

This will initialize the graph. There are two parameters which you should pass. One is the graph my_g1 second is the directionality of the graph. true if directed false if undirected.

initialize_graph :: proc ( g: ^Graph_t, directed: bool )

Example :

initialize_graph( my_g1, false )

2.Read the graph.

This will read the graph from given file. We have added an example file so you can have a idea about how data should be presented in graph

read_graph :: proc (g : ^Graph_t, filename : string)

Example :

read_graph( myg1, os.args[1] )

3. Insert an edge.

This will insert an edge with weight w to graph g from x to y.

insert_edge :: proc ( g : ^Graph_t, x : int, y : int, w : int )

Example :

insert_edge( my_g1, 1, 2, 10 )

4. Delete an edge.

This will delete the edge of graph

delete_edge :: proc ( g : ^Graph_t, from : int , to : int )

Example :

delete_edge( my_g1, 1, 2 )

5. Print graph.

This function can be used to print the graph. Name of the graph should be name

print_graph :: proc (g : ^Graph_t , name : string )

Example :

print_graph( my_g1, "my_g1")

6. Print degree.

This function will print the degree of the given graph

print_degree :: proc (g : ^Graph_t )

Example :

print_degree( my_g1 )

7. Print Complement.

This function will print the compliment grpah of given grpah. set the k value for 1.

print_complement :: proc (g : ^Graph_t, p : int )

Example :

print_complement( my_g1, 1 )

8. Eliminate links.

This function will eliminate the links which have a weight less than minW or more than maxW

eliminate_links :: proc (g : ^Graph_t , minW : int, maxW : int )

Example :

eliminate_links(my_g1, 2, 5 )

9. Different Links.

This will find the different links between graph g and c. graph g will be given priority so this will print the links of g which are not in c

different_links :: proc (g : ^Graph_t , c : ^Graph_t )

Example :

different_links( my_g1, my_g2 )

10. Common Links.

This will find and print the common links of graph g and graph c

common_links :: proc (g : ^Graph_t , c : ^Graph_t )

Example :

common_links( my_g2, my_ g1 )

11. Depth First Search.

This function will perform the Depth First Search starting from node k

dfs :: proc ( g : ^Graph_t, k : int )

Example :

dfs( my_g1, 1 )

12. Breadth First Search.

This function will perform the Breadth First Search starting from node k

bfs :: proc ( g : ^Graph_t, k : int )

Example :

bfs( my_g1, 1 )

13. Check the connectivity of a graph.

This will return True if the graph is connected of False if not

is_connected :: proc ( g : ^Graph_t ) -> bool

Example :

connected : bool = is_connected( my_g1 )

14. Check the number of connected components.

This will print the number of connected components in the graph

num_of_conn_comp :: proc (g : ^Graph_t )

Example :

num_of_conn_comp( my_g1 )

15. Copy a graph.

This will copy the give graph and will return the pointer to the newly created graph

copy_graph :: proc (g : Graph_t) -> ^Graph_t

Example :

my_g2 = copy_graph( my_g1 )

16. free a graph.

Free's the memory used by the graph.

free_graph :: proc ( g : ^Graph_t )

Example :

free_graph( my_g1 )

Best regards

Joao Carvalho