Graphs in data Structures, Slides of Data Structures and Algorithms

Graps,Directed,Undirected graphs,Graph implementation

Typology: Slides

2018/2019

Uploaded on 06/26/2019

monaa_pin
monaa_pin 🇮🇳

4 documents

1 / 41

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Graphs
Data Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Graphs in data Structures and more Slides Data Structures and Algorithms in PDF only on Docsity!

Graphs

Data Structures

What is a graph?

  • A data structure that consists of a set of nodes ( vertices ) and a set of edges that relate the nodes to each other
  • The set of edges describes relationships among the vertices

Directed vs. undirected graphs

  • When the edges in a graph have no

direction, the graph is called undirected

  • When the edges in a graph have a direction,

the graph is called directed (or digraph )

Directed vs. undirected graphs (cont.) E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) Warning : if the graph is directed, the order of the vertices in each edge is important !!

Graph terminology

  • Adjacent nodes: two nodes are adjacent if they are connected by an edge
  • Path: a sequence of vertices that connect two nodes in a graph
  • Complete graph: a graph in which every vertex is directly connected to every other vertex 5 is adjacent to 7 7 is adjacent from 5
  • What is the number of edges in a complete directed graph with N vertices? N * (N-1) Graph terminology (cont.) 2 O N ( )
  • Weighted graph: a graph in which each edge carries a value Graph terminology (cont.)

Graph implementation

  • Array-based implementation
    • (^) A 1D array is used to represent the vertices
    • (^) A 2D array (adjacency matrix) is used to represent the edges

Graph implementation (cont.)

  • Linked-list implementation
    • (^) A 1D array is used to represent the vertices
    • (^) A list is used for each vertex v which contains the vertices which are adjacent from v (adjacency list)

Linked-list implementation

Graph searching

  • Problem :^ find a path between two nodes of

the graph (e.g., Austin and Washington)

  • Methods :^ Depth-First-Search^ (DFS)^ or

Breadth-First-Search (BFS)

Depth-First-Search (DFS)

  • What is the idea behind DFS?
    • (^) Travel as far as you can down a path
    • (^) Back up as little as possible when you reach a "dead end" (i.e., next vertex has been "marked" or there is no next vertex)
  • DFS can be implemented efficiently using a

stack

start end (initialization)