Saturday 13 May 2017

Basics of Graph data structure

  • Graph consist of vertices and edges.
  • They are used to model many realworld examples ( likes cities, network…)


Graph



Implementing a graph with 4 vertices (A,B,C,D) and 4 edges.





Let’s start with creating vertex class. Here isVisited data member will help us to know already visited vertices ( DFS,BFS ).



Now, let’s have Graph class with constructor that will us for initializing vertices and their connection (edges) and 2 methods, one for creating vertex instance & other for making connection between them.




Implement addVertex method, which will help us to create new vertex instance that will be stored in an array.



Implement connectVertices method, where connection between vertices will be stored in 2D matrix.



Finally, you can call above methods in your main method.


Basics of Tree data structure

Tree data structure simulates a  hierarchical tree structure, with root and subtrees represented by linked nodes. Some Terminology Root...