> For the complete documentation index, see [llms.txt](https://university-west.gitbook.io/programming-paradigms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/data-structures/graphs.md).

# Graphs

A **graph** is a data structure that consists of a set of **nodes** (also known as vertices) and a set of **edges** that connect pairs of nodes. Graphs are used to represent networks of connections or relationships between objects, making them incredibly useful in various applications, from social network analyses to route optimization and beyond.

Graphs can be **directed** or **undirected**. In a directed graph, edges have a direction, indicating a one-way relationship, while in an undirected graph, the edges do not have a direction, signifying a bi-directional relationship. Additionally, graphs can be **weighted** or **unweighted**, where a weight represents the cost, distance, or any measurable attribute associated with traversing from one node to another.

#### Example in Python

In Python, graphs can be represented using dictionaries, where keys represent nodes, and values represent lists of adjacent nodes. Here's a simple example of an undirected graph:

```python
graph = {
    'A': ['B', 'C'],
    'B': ['A', 'D', 'E'],
    'C': ['A', 'F'],
    'D': ['B'],
    'E': ['B', 'F'],
    'F': ['C', 'E']
}

print(graph)
```

This snippet defines a graph and prints it, showcasing the connections between nodes.

#### Example in C\#

In C#, one way to represent a graph is by using a class to define a node and a list to track its edges or adjacent nodes. Here is a simple implementation:

```csharp
using System;
using System.Collections.Generic;

public class GraphNode {
    public string Label;
    public List<GraphNode> Adjacent = new List<GraphNode>();

    public GraphNode(string label) {
        Label = label;
    }

    public void Connect(GraphNode node) {
        Adjacent.Add(node);
    }
}

class Program {
    static void Main() {
        GraphNode a = new GraphNode("A");
        GraphNode b = new GraphNode("B");
        GraphNode c = new GraphNode("C");

        a.Connect(b);
        b.Connect(c);
        c.Connect(a);

        Console.WriteLine($"{a.Label} is connected to {a.Adjacent.Count} nodes.");
    }
}
```

This code demonstrates creating a simple graph with three nodes and connecting them together. Through these examples, you can appreciate the versatility and power of graphs in solving complex programming challenges.


---

# 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://university-west.gitbook.io/programming-paradigms/programming-fundamentals/data-structures/graphs.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.
