diff --git a/ruby/computer_science/project_graph.md b/ruby/computer_science/project_graph.md new file mode 100644 index 00000000000..67dde031321 --- /dev/null +++ b/ruby/computer_science/project_graph.md @@ -0,0 +1,131 @@ +### Introduction + +A graph is a way to represent connections between things. Think of it like drawing points (called vertices or nodes) and connecting them with lines (called edges). Graphs have a wide variety of applications, and we can use them to model complex relationships. For example: + +- In a social network, each person is a vertex, and friendships are edges +- In a road map, cities are vertices, and roads between them are edges +- In a computer network, devices are vertices, and connections between computers are edges + + + +### Why Use Graphs? + +Graphs are incredibly useful for modeling relationships and connections. They help us solve real-world problems like: + +1. Finding the shortest route between two cities. +1. Suggesting friends on social media. +1. Planning computer network layouts for reliability and speed. +1. Analysing and managing dependencies when bundling code. +1. Ranking pages based on connections to similar pages by search engines. + +There are a handful of types of graphs used to solve this wide variety of problems. Graphs can be either *directed* or *undirected*, and can be either *weighted* or *unweighted*. A quick explanation of these terms: + +- Directed: Connections go only one way (if A connects to B, B doesn't necessarily connect to A). Dependencies in our code are directed, since module A importing B means that module B cannot import module A without introducing circular dependencies. +- Undirected: Connections go both ways (if A connects to B, B connects to A). A computer network is likely to be undirected, since connections between any two computers are mutual. +- Weighted: Connections have some numeric weight that specifies something about them. A road map is likely to be weighted, where the weights are the distances between the cities. This allows you to calculate the distance of your journey by adding the weights of the roads along the way. +- Unweighted: All connections are equal - no one connection is more important than any other. A social network is likely to be unweighted, since connections signify only that a friendship exists. + +In this project, we will be using the simplest type of graph, an undirected, unweighted graph. + +### Representing a Graph + +In our code, there are several ways we could represent a graph. Two of the most common representations include *adjacency lists* and *adjacency matrices*. Read this article about [graphs and their representations](https://www.geeksforgeeks.org/graph-and-its-representations/) from GeeksforGeeks to familiarise yourself with these ideas. They have some example code, but don't pay too much attention to this, as it's a little different to the code we'll be writing in this project. + +In this project, we'll be using an adjacency list to represent the graph. We've picked this style of graph because it generally requires less manual bookkeeping when adding or removing vertices, making it easier to keep the graph’s state consistent. Adjacency lists also use less space for many real-world graphs and make it simpler to work with a vertex’s immediate neighbors. For these reasons, adjacency lists are often a better fit for modeling the kinds of relationships you'll encounter in practice. If you would like, you can read this article about [the differences between using adjacency lists and adjacency matrices](https://www.geeksforgeeks.org/dsa/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/) + +### Assignment + +