Sunday, August 16, 2020

Graph Representations & Graph Traversals

Graphs have two primary components known as i) vertex and ii) a pair of Vertex bonding to form an edge.

Graphs can be represented in two different ways.
  1. Adjacency Matrix : The Distance between two vertices are represented in a row X column format.
  2. Adjacency List: An Array of linked Lists are maintained representing each node and its respective edges with other nodes.

The method to traverse a Graph is as follows: 
  1. Breadth First Traversal/Search
  2. Depth First Traversal/Search

Breadth First Traversal(BFS):


Breadth First Traversal is used to traverse all the nodes in a graph, and the order of it is determined using a Queue structure.


Depth First Traversal(DFS):


Depth First Traversal uses a a stack/recursive method approach to traverse all the nodes in a graph.

DFS can be implemented using a recursive method.
This post was originally drafted on 13th Feb 2018.

Django & Connecting it to MySQL

Connection to MySQL Database was a bit challenging task for me.


Background:

I started my Django experience with connection to the default database which is extremely light weight i.e. SQLite. This database gets locked at connecting to it more than once.

In my situation, the db got locked when I was trying to access it from a DB browser while having the Django server running. 

To make things more dynamic and to learn MySQL I began mulling on the idea to setup it up along with Django. 

Now back to the problem.
You primarily have to deal with setting.py under your base directory (BASE_DIR). 

In settings.py file of the Django Project, replace the sqlite3 engine with mysql engine as shown below.



Below given is the reference to mysql engine.


[Note: If you are migrating to MySQL after building the project with any other database, ensure that the migration folder under the apps are deleted.]

References are mentioned below. 

[Originally drafted on 6th August 2018]




Wednesday, January 17, 2018

Introduction to Data Structures

A data structure is a specialized format for organizing and storing data in a computer so that it can be used efficiently.Every data needs to be stored and retrieved and given a form or a structure.

General data structure types include the array, the file, the record, the table, the tree, and so on. Any data structure is designed to organize data to suit a specific purpose so that it can be accessed and worked with in appropriate ways.

For example: You can assume roads in a map or database of a company or anything else which needs to be programmed in a particular format to conform it to a structure.

Data structure mainly specifies the structured organization of data, by providing accessing methods with correct degree of associativity.Data structures affects the design of both the structural and functional aspects of a program. An algorithm with a data structure makes a functioning program.Here the data structures are the building blocks of a program.


The efficiency and volume of a data structure is steadily increasing with everyday.Hence there are various methods and ways to achieve the most efficient and perfectly designed program.From the above discussions we come to a conclusion that a data structure is an organized data along with predefined set of operations.

Monday, January 30, 2017

Backtracking Algorithms

Backtracking Algorithm as the name suggests is an algorithm which backtracks for solutions and works for problems with constraints satisfaction.Recursion is the key method to Backtracking algorithms. Here we use recursion in an incremental way to tackle the problem.For example, N Queens problem or Knight's tour problem. Say at any point in the stack, the  function fails to tackle the problem and returns false, the predecessor function is invoked (i.e. backtracked) and the necessary functional block takes care of the problem.





I have attempted few problems from Geeks4Geeks for an indepth understanding of Backtracking algorithms.
[Refer to this]

  1. Knight's Tour problem [1]
  2. Printing all permutations of a string.

  1. Rat in a Maze Problem.[1]
  2. Sudoku Algorithm [1]
  3. N Queens Problem [1]

Friday, December 30, 2016

Heap Sort

Heap sort is a sorting method on Binary Heap.

First things First

All you need to know about heap. 
  1. Heap must be a Complete Binary Tree 
  2. All trees are either Min Heap or Max Heap
  3. Heap sort is an in-place sort algorithm
  4. It is not very stable
Here I plan to construct Heap Sort Algorithm on a Min Heap.The Algorithm primarily has two main functions.
  1. Heapify Function
  2. Heap Sort Function

References from CLRS 

 Parent(i) 1 return(floor(i/2))
 Left (i)    1 return ( 2 * i + 1 )
 Right (i)  1 return ( 2 * i + 2 )

 Property for Max Heap 
    A[ Parent ] >= A[ i ]

 Property for Min Heap
     A[ Parent ] <= A[ i ]

Heapify Function



Heap Sort Function





Deletion of node is done at the root node after swapping it with the last node of the list. Deleting it takes O(1) time complexity.
Worst Case Time Complexity : O(n log n)
Space Complexity : O(n)
Source Code: HeapSort.cpp
References: StudyTonight

Wednesday, December 28, 2016

Naive Pattern Searching Algorithm

Naive Pattern Search algorithm as the name suggests is a very naive method of searching a pattern. The naive logic is to compare every instance of the pattern with every instance of the text.

Algorithm



Naive algorithm is not an optimized solution because the complexity is O(n^2). We need better and more optimized solutions.

Source Code: NaiveSearchAlgo

Monday, March 7, 2016

Insertion Sort

Insertion Sort algorithm is a sort algorithm which is used to sort an array in a particular order.
It is very less efficient with large lists.

Properties:

  1. Stable 
  2. O(1) ExtraSpace 
  3. O(n^2) comparisons & Swap 
  4. Adaptive: O(n) time when nearly sorted
  5. Very low overhead
Insertion sort is an algorithm of choice when the data is nearly sorted or when the problem size is small. 

Algorithm




Example 

The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the key under consideration is underlined. The key that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold.

3 7 4 9 5 2 6 1
3 7 4 9 5 2 6 1
7 4 9 5 2 6 1
4 7 9 5 2 6 1
3 4 7 9 5 2 6 1
3 4 5 7 9 2 6 1
2 3 4 5 7 9 6 1
2 3 4 5 6 7 9 1
1 2 3 4 5 6 7 9
References: 1 
Note: To be edited further with a Code for the algorithm