Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:

Similar presentations


Presentation on theme: "Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:"— Presentation transcript:

1 Graph Representations And Traversals

2 Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination: v

3 Graphs Examples:

4 Terms Undirected graph – Edges go both ways

5 Terms Digraph : Directed graph – Edges can be one direction

6 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

7 Graph Definitions and Notations Parallel edges: Edges between same vertices Simple graph: no loops or parallel edges

8 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

9 Graph Definitions and Notations Connected : there is an undirected path between any 2 nodes Strongly connected : directed path exists between any two nodes

10 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

11 Everything is a digraph Every pointer based data structure is a digraph – Trees & Linked lists = acyclical digraphs

12 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…

13 Representation Adjacency matrix – 2D array indicating edges

14 Representation Undirected graph produces symmetric matrix

15 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)

16 Adjacency Matrix Power of adj matrix shows # of paths of that length

17 Representation Adjacency list : – Store a list of all edges starting from given vertex:

18 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)

19 Other Options Incident Based – Incident Matrix : V rows x E cols Adjacency set – BST for edges related to a vertex

20 BFS BFS : Breadth First Search

21 BFS BFS - Spreads out in all directions – 1 Hop

22 BFS BFS - Spreads out in all directions – 1 Hop

23 BFS BFS - Spreads out in all directions – 1 Hop

24 BFS BFS - Spreads out in all directions – 1 Hop

25 BFS BFS - Spreads out in all directions – 1 Hop

26 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops

27 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops

28 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops

29 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops

30 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops

31 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops

32 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops

33 BFS BFS - Spreads out in all directions – 1 Hop – 2 Hops – 3 Hops – 4 Hops

34 DFS DFS – Depth First Search

35 DFS DFS -

36 DFS DFS -

37 DFS DFS -

38 DFS DFS -

39 DFS DFS -

40 DFS DFS -

41 DFS DFS -

42 DFS DFS -

43 DFS DFS -

44 DFS DFS -

45 DFS DFS -

46 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

47 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

48 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

49 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

50 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

51 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

52 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

53 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

54 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

55 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

56 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

57 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

58 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

59 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

60 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

61 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

62 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

63 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available

64 DFS DFS – Backtrack when stuck – Chain again as soon as a new path available


Download ppt "Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:"

Similar presentations


Ads by Google