> For the complete documentation index, see [llms.txt](https://edsonha.gitbook.io/my-gitbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://edsonha.gitbook.io/my-gitbook/data-structure-and-algorithm/graph.md).

# Graph

A graph is a set of values that are related. There are connections (edges) to different nodes (vertex) in a graph.

![](/files/-LsVTjo1W6JQ8z6WSY2K)

There are 3 things to take note in a graph:

1. Directed vs Undirected. Undirected means I can go back and forth between nodes, like Facebook friends. Directed means I can only go in a specific direction, like Twitter where you follow, but the other person does not automatically follow you
2. Weighted vs Unweighted. Weighted means, besides value in the node, we can also have information on the edges. These sort of graphs are used a lot in calculating optimal paths / fastest way to destination.
3. Cyclic vs acyclic. Cyclic is the idea where the nodes are all interconnected and the idea is you can go back to the start

There are 3 different ways to represent graph:

![](/files/-LsVVfXVQFmeIQUri5px)

* Edge List - simply shows the connection

```javascript
const edgeList = [[0,2], [1,2], [2,3], [1,3]]
```

* Adjacent List - create a graph where the index is the node and the value is the nodes neighbors

```javascript
const adjacentList = { 0: [2], 1: [2,3], 2: [0,1,3], 3: [1,2]}
//Node 0 is connected to 2, node 1 is connected to 2 and 3...
```

* Adjacent Matrix -  the matrix is going to have zeros and ones indicating whether the node X has a connection to node Y. Zero means no connection and One means yes. And if you have a weighted graph, you can actually add weights here, instead of 1 and 0.

```javascript
 const graph = { 
  0: [0, 0, 1, 0], // index 0 has connection with 2 
  1: [0, 0, 1, 1], // index 1 has connection with 2 and 3 
  2: [1, 1, 0, 1], // index 2 has connection with 0, 1 and 3 
  3: [0, 1, 1, 0]  // index 3 has connection to 1 and 2
}
```

In the graph exercise, it is more useful to use object rather than array. Remember shifting on array is expensive. With objects we can quickly find items.

Pro of Graph: There are relationships

Cons of Graph: They can get complicated, thus scaling is hard


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://edsonha.gitbook.io/my-gitbook/data-structure-and-algorithm/graph.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
