General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Data structures

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:

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:

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.

PreviousTreesNextControl structures

Last updated 1 year ago

Was this helpful?