Quick actions

cmd+k|ctrl+k

Navigation

Languages

Udemy: Master Coding Interview - Trees - #144

Snippet info

Language

Csharp

Visibility

public

Author

jcurrie33

Created

2020-08-11T11:55:52Z

Updated

2020-08-11T11:55:52Z

using System;
using System.Collections.Generic;


class Graph {
    public Dictionary<int, List<int>> adjacencyList;
    
    static void Main() {
        Console.WriteLine("Hello World!");
        
        Graph g = new Graph();
        g.AddVertex(0);
        g.AddVertex(1);
        g.AddVertex(2);
        g.AddVertex(3);
        g.AddVertex(4);
        g.AddVertex(5);
        g.AddVertex(6);
        g.AddEdge(0, 1);
        g.AddEdge(0, 2);
        g.AddEdge(1, 2);
        g.AddEdge(1, 3);
        g.AddEdge(2, 4);
        g.AddEdge(3, 4);
        g.AddEdge(4, 5);
        g.AddEdge(5, 6);
        Console.WriteLine(g.ToString());
    }
    
    public Graph() {
        this.adjacencyList = new Dictionary<int, List<int>>();
    }
    
    public void AddVertex(int vertex) {
        this.adjacencyList[vertex] = new List<int>();
    }
    
    public void AddEdge(int vertex1, int vertex2) {
        this.adjacencyList[vertex1].Add(vertex2);
        this.adjacencyList[vertex2].Add(vertex1);
    }
    
    public string ToString() {
        string s = "Graph Adjacency List:\n";
        foreach (KeyValuePair<int, List<int>> entry in this.adjacencyList) {
            s += "" + entry.Key + ":";
            foreach (int v in entry.Value) {
                s += " " + v;
            }
            s += "\n";
        }
        return s;
    }
}
INFO