Skip to content

GH-292: Graph in C++ #293

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

Merged
merged 4 commits into from
Apr 7, 2023
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
62 changes: 62 additions & 0 deletions concepts/cpp/graph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Graph

A `graph` is a data structure that consists of the following two components:

* A finite set of vertices also called as **nodes**.
* A finite set of ordered pair of the form `(u, v)` called as **edge**. The pair is ordered because `(u, v)` is not same as `(v, u)` in case of a **directed graph(di-graph)**. The pair of the form `(u, v)` indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.

## Graph Representation in C++

Example graph:

```
1----->2----->3
^ | |
| | |
| v v
4<-----5<-----6
```

The most common way to represent a graph is using an `adjacency list`. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. The node is represented by the index of the list. The neighbors are the elements of the list. The following is an adjacency list representation of the graph above.


```c++
#include <iostream>
#include <vector>

using namespace std;
```


```c++
vector<vector<int>> adj(7); // 1-indexed

adj[1] = {2};
adj[2] = {3, 5};
adj[3] = {6};
adj[4] = {1};
adj[5] = {4};
adj[6] = {5};

for(int i=1; i<adj.size(); i++){
cout << i << ": ";
for(int j=0; j<adj[i].size(); j++){
cout << adj[i][j] << " ";
}
cout << endl;
}

```

1: 2
2: 3 5
3: 6
4: 1
5: 4
6: 5


## References

* [Graph](https://en.wikipedia.org/wiki/Graph_(abstract_data_type)), wikipedia
* ▶️ [Graph Algorithms for Technical Interviews - Full Course](https://www.youtube.com/watch?v=tWVWeAqZ0WU&ab_channel=freeCodeCamp.org), freeCodeCamp.org
134 changes: 134 additions & 0 deletions concepts/cpp/graph/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graph\n",
"\n",
"A `graph` is a data structure that consists of the following two components:\n",
"\n",
"* A finite set of vertices also called as **nodes**.\n",
"* A finite set of ordered pair of the form `(u, v)` called as **edge**. The pair is ordered because `(u, v)` is not same as `(v, u)` in case of a **directed graph(di-graph)**. The pair of the form `(u, v)` indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graph Representation in C++"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Example graph: \n",
"\n",
"```\n",
" 1----->2----->3\n",
" ^ | |\n",
" | | |\n",
" | v v\n",
" 4<-----5<-----6\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The most common way to represent a graph is using an `adjacency list`. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. The node is represented by the index of the list. The neighbors are the elements of the list. The following is an adjacency list representation of the graph above."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <vector>\n",
"\n",
"using namespace std;"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1: 2 \n",
"2: 3 5 \n",
"3: 6 \n",
"4: 1 \n",
"5: 4 \n",
"6: 5 \n"
]
}
],
"source": [
"vector<vector<int>> adj(7); // 1-indexed\n",
"\n",
"adj[1] = {2};\n",
"adj[2] = {3, 5};\n",
"adj[3] = {6};\n",
"adj[4] = {1};\n",
"adj[5] = {4};\n",
"adj[6] = {5};\n",
"\n",
"for(int i=1; i<adj.size(); i++){\n",
" cout << i << \": \";\n",
" for(int j=0; j<adj[i].size(); j++){\n",
" cout << adj[i][j] << \" \";\n",
" }\n",
" cout << endl;\n",
"}\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"* [Graph](https://en.wikipedia.org/wiki/Graph_(abstract_data_type)), wikipedia\n",
"* ▶️ [Graph Algorithms for Technical Interviews - Full Course](https://www.youtube.com/watch?v=tWVWeAqZ0WU&ab_channel=freeCodeCamp.org), freeCodeCamp.org"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
4 changes: 2 additions & 2 deletions concepts/general/graph/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Graph

*See implementation in*
C++,
[C++](/concepts/cpp/graph/README.md),
Java,
Python,
typescript

In computer science, a graph is a data structure that consists of the following two components:
A graph is a data structure that consists of the following two components:

* A finite set of vertices also called as **nodes**.
* A finite set of ordered pair of the form `(u, v)` called as **edge**. The pair is ordered because `(u, v)` is not same as `(v, u)` in case of a **directed graph(di-graph)**. The pair of the form `(u, v)` indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.
Expand Down
3 changes: 2 additions & 1 deletion readme/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ A data structure is a data organization, management, and storage format that is
</ul>
</li>
<li>
<code>A</code> Graph
<code>A</code>
<a href="/concepts/cpp/graph/README.md">Graph</a>
</li>
<li>
<code>A</code>
Expand Down