Graphs
Telerik Academy Alpha

 

DSA

 Table of contents

What is a graph?

 What is a graph?

  • Set of nodes with many-to-many relationship between them is called graph
    • Each node has multiple predecessors
    • Each node has multiple successors

 Where we use graphs?

  • Few important real life applications of graph data structures are:

    • Facebook: Each user is represented as a vertex(node) and two people are friends when there is an edge between two vertices. Similarly friend suggestion also uses graph theory concept.

    • Google Maps: Various locations are represented as vertices(nodes) and the roads are represented as edges and graph theory is used to find shortest path between two nodes.

 Where we use graphs?

  • Friends

  • Different Connections

 Where we use graphs?

  • Few important real life applications of graph data structures are:

    • Recommendations on e-commerce websites: The “Recommendations for you” section on various e-commerce websites uses graph theory to recommend items of similar type to user’s choice.

    • Graph theory is also used to study molecules in chemistry and physics.

Graph definitions

 Node, edge

  • Node(vertex)
    • Element of graph
    • Can have name or value
    • Keeps a list of adjacent nodes
  • Edge
    • Connection between two nodes
    • Can be directed / undirected
    • Can be weighted / unweighted
    • Can have name / value

 Directed graph

  • Directed graph
    • The edges have directions

 Directed graph applications

graph vertex edge
financial stock  currency  transaction
transportation street intersection, airport highway, airway route
telephone person placed call

One-way streets

 Undirected graph

  • Undirected graph
    • The edges have NO directions

 Undirected graph applications

graph vertex edge
communication telephones, computers fiber optic cables
mechanical joints rods, beams, springs
financia stocks, currency transactions

Friend connections

 Weighted graph

  • Weight (cost) is associated with each edge

 Paths

  • Path (in undirected graph)
    • Sequence of nodes \( n_1, n_2, ..., n_k \)
    • Edge exists between each pair of nodes \( ni, ni+1 \)
    • Examples:
      • A,B,C is a path
      • H,K,C is not a path

 Paths

  • Path (in directed graph)
    • Sequence of nodes \( n_1, n_2, ..., n_k \)
    • Direct edge exists between each pair of nodes \( ni, ni+1 \)
    • Examples:
      • A,B,C is a path
      • A,G,K is not a path

 Cycle

  • Cycle
    • Path that ends back at the starting node
    • Example:
      • A,B,C,G,A
    • Simple path
      • No cycles in path
    • Acyclic graph
      • Graph with no cycles
      • Acyclic undirected graphs are trees

 Connections

  • Two nodes are reachable if
    • Path exists between them
  • Connected graph
    • Every node is reachable from any other node

 Connections

  • Connected graph
  • Unconnected graph

Representing graphs

 Representing graphs

  • Adjacency list
    • Each node holds a list of its neighbors
  • Adjacency matrix
    • Each cell keeps whether and how two nodes are connected
  • Set of edges

 C# Representation - simple

using System.Collections.Generic;
					
public class Program
{
    public static void Main()
    {
	Graph g = new Graph(new List<int>[] {
	  new List<int> {3, 6}, // successors of vertice 0
	  new List<int> {2, 3, 4, 5, 6},// successors of vertice 1
	  new List<int> {1, 4, 5}, // successors of vertice 2
	  new List<int> {0, 1, 5}, // successors of vertice 3
	  new List<int> {1, 2, 6}, // successors of vertice 4
	  new List<int> {1, 2, 3}, // successors of vertice 5
	  new List<int> {0, 1, 4}  // successors of vertice 6
	});
    }
}

public class Graph
{
     List<int>[] childNodes;
     public Graph(List<int>[] nodes)
     {
        this.childNodes = nodes;
     }

}

 C# Representation - advanced

  • Write your own using OOP (as an exercise):
    • Class Node
    • Class Connection (Edge)
    • Class Graph
    • Optional classes
  • Using external library (redommended):

Questions?

[C# DSA] Graphs

By telerikacademy

[C# DSA] Graphs

  • 1,122