Skip to content
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

Markdown fixes #736

Merged
merged 2 commits into from
Feb 25, 2024
Merged
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
76 changes: 61 additions & 15 deletions Data Structures/Array.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
# Array
# [Array](#array)

* An array is a collection of items of same data type stored at contiguous memory locations.

## [Table of Contents](#table-of-contents)

- [Array](#array)
- [Table of Contents](#table-of-contents)
- [Example](#Example)
- [Initalization of Array](#initalization-of-array)
- [Time complexities of some basic array operations](#time-complexities-of-some-basic-array-operations)
- [Algorithms on Arrays](#algorithms-on-arrays)
- [Searching](#searching)
- [1. Linear Search](#1-linear-search)
- [2. Binary Search](#2-binary-search)
- [Insertion](#insertion)
- [Insertion at the beginning of an Array](#insertion-at-the-beginning-of-an-array)
- [Insertion at the Given Index of an Array](#insertion-at-the-given-index-of-an-array)
- [Insertion at the end of the Array](#insertion-at-the-end-of-the-array)
- [Deletion](#deletion)
- [Delete Operation in an Unsorted Array](#delete-operation-in-an-unsorted-array)
- [When to Use Arrays](#when-to-use-arrays)

* This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset.

## Example
## [Example](#Example)

```javascript
let array = [4,3,8,1,0,14,6];
Expand Down Expand Up @@ -36,9 +55,9 @@ array[6] // 6 (index 6 is the seventh element) (1006)
<img src="images/Array.png?raw=true" alt="Array Image"/>
</p>

## Initalization of Array
## [Initalization of Array](#initalization-of-array)

* To initilaize an array it is necessary to specify its size:
* To initialize an array it is necessary to specify its size:

In C++, for example, here is how we initialize an array of 5 elements:

Expand All @@ -49,7 +68,7 @@ int arr[5];

this will initialize an array of 5 elements with the value 0.

## Time complexities of some basic array operations
## [Time complexities of some basic array operations](#time-complexities-of-some-basic-array-operations)

| Operation | Time Complexity |
|-----------------|-------|
Expand All @@ -60,11 +79,11 @@ this will initialize an array of 5 elements with the value 0.

* Inserting and deleting elements take O(n) time because we need to shift the remaining data points either right or left after inserting or deleting them, in order to preserve the memory indexing.

## Algorithms on Arrays
## [Algorithms on Arrays](#algorithms-on-arrays)

### Searching
### [Searching](#searching)

### 1. Linear Search
### [1. Linear Search](#1-linear-search)

Algorithm :

Expand All @@ -80,7 +99,7 @@ Algorithm :

print "Element Not Found".

### 2. Binary Search
### [2. Binary Search](#2-binary-search)

Algorithm :

Expand Down Expand Up @@ -111,7 +130,7 @@ print "value is not present in the array"
[end of if]
Step 6: exit

## Insertion
## [Insertion](#insertion)

It basically means inserting an element inside an array.
There are following types of Insertions in Arrays:
Expand All @@ -121,7 +140,7 @@ There are following types of Insertions in Arrays:
* Insertion at the end of the Array


### Insertion at the beginning of an Array
### [Insertion at the beginning of an Array](#insertion-at-the-beginning-of-an-array)

```C
#include <stdio.h>
Expand Down Expand Up @@ -176,7 +195,7 @@ array[3] = 4
array[4] = 5
```

### Insertion at the Given Index of an Array
### [Insertion at the Given Index of an Array](#insertion-at-the-given-index-of-an-array)

```C
#include<stdio.h>
Expand Down Expand Up @@ -224,7 +243,7 @@ Enter position and element
1 20 5 78 30 5
```

### Insertion at the end of the Array
### [Insertion at the end of the Array](#insertion-at-the-end-of-the-array)

```C
#include <stdio.h>
Expand Down Expand Up @@ -259,13 +278,13 @@ void main()
<img src="https://quescol.com/wp-content/uploads/2021/11/image-35.png?ezimgfmt=ng:webp/ngcb1" alt="output">
<br>

## Deletion
## [Deletion](#deletion)

* Deletion refers to removing an existing/given element from the array using indexes and re-organizing all elements of an array.

* In the delete operation, the element to be deleted is searched using the **linear search**, and then the delete operation is performed followed by shifting the elements.

### Delete Operation in an Unsorted Array
### [Delete Operation in an Unsorted Array](#delete-operation-in-an-unsorted-array)

```C++

Expand Down Expand Up @@ -327,3 +346,30 @@ int main()
return 0;
}
```


## [When to Use Arrays](#when-to-use-arrays)

- Sequential Access Requirements:
- If you need to access elements in a sequential manner and the order of elements is important, arrays are a suitable choice. Elements in an array are stored in contiguous memory locations, making sequential access efficient.

- Fixed Size Data Storage:
- When the number of elements is known and fixed, arrays provide a straightforward way to store and access the data. The fixed size of arrays ensures that a specific amount of memory is allocated, making them efficient for static data structures.

- Random Access Needs:
- If you need fast and direct access to elements based on their position (index), arrays are well-suited. Accessing an element by index has constant time complexity O(1).

- Efficient Memory Usage:
- Arrays are memory-efficient as they allocate a contiguous block of memory for elements. This makes them suitable for scenarios where memory utilization needs to be optimized.

- Simple Data Structures:
- For simple data structures like lists of items or collections where elements are of the same type, arrays provide a straightforward and easy-to-understand solution.

- Implementation of Matrices and Multidimensional Data:
- In cases where you need to represent matrices or multidimensional data, arrays offer a natural and efficient representation. Each dimension can be addressed using a separate index.

- Efficient Iteration:
- If you plan to iterate through the elements of a collection sequentially, arrays are efficient for loop-based operations. Iterating over the elements is fast and can be done using simple loop constructs.

- Implementation of Other Data Structures:
- Arrays serve as the foundation for implementing other data structures like stacks, queues, and dynamic arrays (lists). Many dynamic data structures use arrays to manage and organize their elements.
56 changes: 49 additions & 7 deletions Data Structures/Graph.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Graph
# [Graph](#graph)

A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by G(E, V).

Expand All @@ -7,8 +7,18 @@ Components of a Graph :
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known as vertex or nodes. Every node/vertex can be labeled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no rules. Sometimes, edges are also known as arcs. Every edge can be labeled/unlabelled.

## [Table of Contents](#table-of-contents)
[Graph](#graph)
- [Example of a Graph](#example-of-a-graph)
- [Graph Traversal](#graph-traversal)
- [1. Breadth First Search (BFS)](#1-breadth-first-search-bfs)
- [BFS Code Example](#bfs-code-example)
- [2. Depth First Search (DFS)](#2-depth-first-search-dfs)
- [DFS Code Example](#dfs-code-example)
- [Graph Use Cases](#graph-use-cases)

### Example

### [Example of a Graph](#example-of-a-graph)
![](https://cdn.programiz.com/sites/tutorial2program/files/graph-vertices-edges_0.png)
```
In the graph,
Expand Down Expand Up @@ -63,11 +73,11 @@ int main()
}
```

## Graph Traversal :
## [Graph Traversal](#graph-traversal)

There are two types of traversal in graph

#### 1. Breadth First Search (BFS)
### [1. Breadth First Search (BFS)](#1-breadth-first-search-bfs)
* In BFS traversal , we start at the root of the graph and visits all nodes at the current depth level before moving on to the nodes at the next depth level.

BFS Implementation:
Expand All @@ -80,7 +90,7 @@ There are two types of traversal in graph
* In this way, all the nodes are traversed in a breadthwise manner.


### Example
#### [BFS Code Example](#bfs-code-example)
![](https://cdn.programiz.com/sites/tutorial2program/files/graph-vertices-edges_0.png)

> Code for BFS Traversal :
Expand Down Expand Up @@ -156,7 +166,7 @@ Time Complexity: O(N) + O(2E), Where N = Nodes, 2E is for total degrees as we tr
Space Complexity: O(3N) ~ O(N), Space for queue data structure visited array and an adjacency list
Space Complexity: O(3N) ~ O(N), Space for queue data structure visited array and an adjacency list

#### 2. Depth First Search (DFS)
### [2. Depth First Search (DFS)](#2-depth-first-search-dfs)
* In DFS, concept of recursion and backtracking is used. DFS goes in-depth, i.e., traverses all nodes by going ahead, and when there are no further nodes to traverse in the current path, then it backtracks on the same path and traverses other unvisited nodes.

DFS Implementation:
Expand All @@ -167,7 +177,7 @@ Space Complexity: O(3N) ~ O(N), Space for queue data structure visited array and

In this way, all the nodes are traversed in a depthwise manner.


#### [DFS Code Example](#dfs-code-example)

> Code for DFS Traversal :
```cpp
Expand Down Expand Up @@ -236,3 +246,35 @@ Output: 0 1 2 3
Time Complexity: For an undirected graph, O(N) + O(2E), For a directed graph, O(N) + O(E), Because for every node we are calling the recursive function once, the time taken is O(N) and 2E is for total degrees as we traverse for all adjacent nodes.

Space Complexity: O(3N) ~ O(N), Space for dfs stack space, visited array and an adjacency list.

## [Graph Use Cases](#graph-use-cases)

- Social Networks:
- Graphs are widely used to model social networks, where individuals are represented as nodes, and connections or friendships between them are represented as edges. Algorithms on graphs can analyze network structures, identify influential nodes, and recommend connections.

- Network Routing:
- Graphs play a crucial role in computer networks. Nodes represent routers or computers, and edges represent connections or links between them. Graph algorithms help optimize routing paths, manage network congestion, and ensure efficient data transmission.

- Recommendation Systems:
- Recommendation systems often use graphs to model relationships between users and items. Nodes can represent users or products, and edges can represent interactions or preferences. Graph-based algorithms help identify related items for personalized recommendations.

- Geographical Information Systems (GIS):
- Graphs are used to model spatial relationships in GIS applications. Nodes represent locations, and edges represent connections like roads or pathways. Graph algorithms can find the shortest path between two locations, optimize routes, and analyze geographical networks.

- Circuit Design and Analysis:
- Electrical circuits can be modeled as graphs, with nodes representing components and edges representing connections. Graph algorithms help analyze circuit behavior, optimize designs, and identify critical components.

- Dependency Analysis:
- In software development, graphs can represent dependencies between modules or functions. Nodes represent code entities, and edges represent dependencies. Dependency graphs help identify the impact of changes, manage software versions, and optimize build processes.

- Internet Web Pages:
- The World Wide Web can be represented as a directed graph, where web pages are nodes, and hyperlinks are edges. Graph algorithms, such as PageRank, are used to analyze the importance and relevance of web pages for search engine ranking.

- Scheduling and Task Dependency:
- Graphs are employed in scheduling tasks and project management. Nodes represent tasks, and edges represent dependencies between tasks. Graph algorithms help determine the optimal order of task execution and identify critical paths.

- Genetics and Bioinformatics:
- Graphs are used to model relationships in biological data. For example, nodes can represent genes, and edges can represent interactions between genes. Graph algorithms help analyze biological networks, identify gene functions, and study genetic interactions.

- Transportation Networks:
- Graphs model transportation systems, where nodes represent locations (such as cities or stops), and edges represent transportation routes (roads, railways, or flight paths). Graph algorithms help optimize routes, manage traffic, and improve transportation efficiency.
Loading
Loading