15

Click here to load reader

Lecture8 data structure(graph)

Embed Size (px)

DESCRIPTION

Graph Data Type Graph Concepts Graph Data Structure Directed/undirected Graph Graph Relationship Graph implementation

Citation preview

Page 1: Lecture8 data structure(graph)

1

GraphData Structure (CS221)

Lecture 8

Abdisalam Issa-Salwe

Taibah University College of Computer Science & Engineering

Computer Science Department

2

Outline

� Graph Data Type

� Graph Concepts

� Graph Data Structure

� Directed/undirected Graph

� Graph Relationship

� Graph implementation

Page 2: Lecture8 data structure(graph)

2

3

Learning Outcomes

� Describe a Graph ADT

� Understand how to implement a Graph

ADT

� Describe algorithms to traverse a graph

4

The Graph ADT

� A graph is a collection of nodes connected by edges

� If an edge e connects vertex u and v, we denote it as (u;

v).if u = v, e is called a loop

Page 3: Lecture8 data structure(graph)

3

5

Graph Concepts

� A tree is an example of a graph

� In a graph a node may be it’s own

ancestor

� In general a graph has no root node

� Formally, a graph is an ordered pair of

sets (N,E)

� Both nodes and edges may have data

values associated

6

Graph Data Structure (cont…)

� Graph is a data structure that consists of a set of nodes and a set of edges that relate the nodes to one another

� It is made up of a set of nodes called vertices and a set of lines called edges (or arcs) that connect the nodes.

� If the edges in the graph have no direction it is called an undirected graph.

� A graph whose edges are directed from one vertex to another is called a directed graph, or digraph

Page 4: Lecture8 data structure(graph)

4

7

Graphs Data Structure (cont…)

• Like a tree, is a NON-Linear structure

consisting of nodes and links between

the nodes

More definition:

Types: Undirected graph

Directed graph

Graphs are often used to solve problems

8

Undirected Graph

• An undirected graph is a set of nodes

and a set of links between the nodes.

• Each NODE is called a VERTEX

• Each LINK is called an EDGE

• The order of the two connected vertices

is unimportant

ex: whether “V0 connects V1” or

“V1 connects V0”

means the same thing

V0E0

V1

Page 5: Lecture8 data structure(graph)

5

9

V3

V0

V1

V2

V4

E0

E2E3

E4

E5E1

Undirected Graph examples

5 vertices

6 edges

10

Undirected Graph examples

5 vertices 6 edges

V3

V0

V1

V2

V4

E0E2

E3

E4

E5

E1

In drawing, the placement of the vertices

is UNIMPORTANT same as previous graph

Page 6: Lecture8 data structure(graph)

6

11

Directed Graph

A directed graph is a finite set of verticestogether with a finite set of edges. If bothare empty then we have an empty graph

V3

V0

V1

V2

V4

V1

V3

SOURCE

TARGET

12

Directed Graph

• Loops: a loop connects a vertex to itself

• Path: is a sequence of vertices P0, P1, P2,… Pn , each adjacent pairs of vertices Pi and Pi+1 are connected by an edge

VertexLOOP

V1 V3

SOURCE TARGET

path

Page 7: Lecture8 data structure(graph)

7

13

Directed Graph

• Multiple edges:a graph may have two or more edges connecting the same two vertices in the same direction

V3

V0

V1

V2

V4

UndirectedDirected

V3

V0

V1

V2

V4

14

Directed Graph

• Simple graph:

• No loops• No multiple edges• Directed• Undirected

• Directed graph representation:

• Using a 2-Dim array• Using a Linked List for each vertex• Using the set class from the C++

Standard Library ( edge sets )

Page 8: Lecture8 data structure(graph)

8

15

Graph Relationship

� Adjacent vertices: Two vertices in a

graph that are connected by an edge

� Path: A sequence of vertices that

connects two nodes in a graph

� Complete graph: A graph in which every

vertex is directly connected to every other

vertex

� Weighted graph: A graph in which each

edge carries a value

16

Graph Relationship (cont…)

� In a complete graph, every vertex is

adjacent to every other vertex.

� If there are N vertices, there will be N * (N

- 1) edges in a complete directed graph

and N * (N - 1) / 2 edges in a complete

undirected graph.

Page 9: Lecture8 data structure(graph)

9

17

Weighted graphWeighted graphs can be used to represent

applications in which the value of the

connection between the vertices is important,

not just the existence of a connection.

18

Complete graph

Page 10: Lecture8 data structure(graph)

10

19

Degree

� In an undirected graph, the degree of a vertex u is the number of edges connected to u.

� In a directed graph, the out-degree of a vertex u is the number edges leaving u, and its in-degree is the number of edges ending at u

� Each vertex is associated with a linked list that enumerates all the vertices that are connected to u

20

Graph Traversals (cont…)

In general:

•Both traversal algorithms

1. Start at one vertex of a graph

2. Process the information contained

at that vertex

3. Move along an edge to process a

neighbor

Page 11: Lecture8 data structure(graph)

11

21

Graph Traversals (cont…)

• The traversal algorithm should be

careful that it doesn’t enter a “Repetitive

Cycle”. Therefore we will mark each vertex as

it is processed..

• When the traversal finishes, all of the vertices

that can be reached from the start vertex have

been processed.

• Some of the vertices may not be processed

because there is NO PATH from the START

VERTEX

22

A network of computers can be representedBy a graph, with • each vertex representing One of the

machines in the network, and • an Edge representing a communication wire

Between the two machines.

Solving a simple problem using a graph

The question of whether one machine can senda message to another machine boils down towhether the corresponding vertices are Connected by a path

Page 12: Lecture8 data structure(graph)

12

23

Solving a simple problem using a graph

V3

V0

V1

V2

16

3

7 2

• Each edge has a NON-Negative Integer value attached to it called the weight or cost of the edgeThe path with the lowest cost is called the Shortest path

V0 to V3 = 1

V3 to V1 = 3

V1 to V2 = 2

Total 6 6

24

Node ADT

� Essential Operations

�create: Element →→→→ Node

�changeElement: Node´Element →→→→ Node

�getElement: Node →→→→ Element

Page 13: Lecture8 data structure(graph)

13

25

Edge ADT

� Essential Operations

• create: Node´Node´Attribute →→→→ Edge

• changeAttribute: Edge´Attribute →→→→ Edge

• getAttribute: Edge →→→→ Attribute

• getNodes: Edge →→→→ Node´Node

• getEdge: Node´Node →→→→ Edge

26

Digraph ADT

� create: → Graph� addNode: Node´Graph → Graph� addEdge: Edge´Graph → Graph� containsNode: Node´Graph → Boolean� containsEdge: Node´Node´Graph → Boolean� removeNode: Node´Graph → Graph� removeEdge: Node´Node´Graph → Graph� numNodes: Graph → Integer� numEdges: Graph → Integer

� create: → Graph� addNode: Node´Graph → Graph� addEdge: Edge´Graph → Graph� containsNode: Node´Graph → Boolean� containsEdge: Node´Node´Graph → Boolean� removeNode: Node´Graph → Graph� removeEdge: Node´Node´Graph → Graph� numNodes: Graph → Integer� numEdges: Graph → Integer

Page 14: Lecture8 data structure(graph)

14

27

Digraph ADT

� Other helpful operations

�an iterator over all nodes in the graph;

�an iterator over all edges in the graph;

�for each node, an iterator over all nodes to which it has an edge.

28

Digraph Implementations

� Edge Set implementation

�Set of Nodes

�Set of Edges - (node, node, attribute)

� Standard method of representing sets

�e.g. hash table, BSTree

Page 15: Lecture8 data structure(graph)

15

29

Digraph Implementations (cont…)

� Adjacency Set (or Adjacency List)

implementation

�Set of Nodes

�Data at each node includes list of adjacent nodes and edge attributes

� Standard method of representing sets

�e.g. hash table, BSTree