Graph Representations And Traversals
Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination: v
Graphs Examples:
Terms Undirected graph – Edges go both ways
Terms Digraph : Directed graph – Edges can be one direction
Graph Definitions and Notations Adjacent: there is an edge from one vertex to the other Incident: if edge e = (u, v) then e is incident on u and v – Loop: edge incident on a single vertex
Graph Definitions and Notations Parallel edges: Edges between same vertices Simple graph: no loops or parallel edges
Graph Definitions and Notations Path: sequence of vertices u 1, u 2,..., u n such that exist edges (u 1, u 2 ), (u 2, u 3 )... (u n, u n-1 ) Connected vertices u and v: there is a path from u to v Simple path: path where all vertices but first and last are distinct
Graph Definitions and Notations Connected : there is an undirected path between any 2 nodes Strongly connected : directed path exists between any two nodes
Terms Spanning Tree : – Tree covering every vertex made with subset of edges Cycle : – Simple path back to starting node Clique : – Group of completely connected nodes
Everything is a digraph Every pointer based data structure is a digraph – Trees & Linked lists = acyclical digraphs
Representation Can represent as objects – Nodes as objects – Edges as pointers – Node maintains vector of pointers But, some graph algorithms benefit from random access to vertices…
Representation Adjacency matrix – 2D array indicating edges
Representation Undirected graph produces symmetric matrix
Efficiency Graph operations described in terms of – E : num edges – V : num verticies Adjacency Matrix – Edges are cheap – Vertices expensive Best for dense graphs Adjacency matrix Storage O(V 2 ) Add vertex O(V 2 ) Add edge O(1) Remove vertex O(V 2 ) Remove edge O(1) Query: are vertices u, v adjacent? (Assuming that the storage positions for u, v are known) O(1)
Adjacency Matrix Power of adj matrix shows # of paths of that length
Representation Adjacency list : – Store a list of all edges starting from given vertex:
Efficiency Adjacency List – Cost based more on edges – Best for sparse graphs Adjacency ListAdjacency matrix Storage O(V + E)O(V 2 ) Add vertex O(1)O(V 2 ) Add edge O(1) Remove vertex O(V + E)O(V 2 ) Remove edge O(E)O(1) Query: are vertices u, v adjacent? (Assuming that the storage positions for u, v are known) O(V) O(1)
Other Options Incident Based – Incident Matrix : V rows x E cols Adjacency set – BST for edges related to a vertex
BFS BFS : Breadth First Search
BFS BFS - Spreads out in all directions – 1 Hop
BFS BFS - Spreads out in all directions – 1 Hop
BFS BFS - Spreads out in all directions – 1 Hop
BFS BFS - Spreads out in all directions – 1 Hop
BFS BFS - Spreads out in all directions – 1 Hop
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops
BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops – 4 Hops
DFS DFS – Depth First Search
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS -
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available
DFS DFS – Backtrack when stuck – Chain again as soon as a new path available